Exemple #1
0
        public GridModel(IQueryable <T> rows, GridStateModel gridState, FilterModel filterModel, string gridID, string refreshJavascriptFunctionName) : base(gridID, refreshJavascriptFunctionName)
        {
            this.FilterModel = filterModel;
            rows             = this.ExecuteFiltersOnRows(rows);
            rows             = this.ExecuteSorting(rows, gridState);

            this.PaginationModel = new GridPaginationModel(rows.Count(), gridState.SelectedPage, gridState.ItemsPerPage, gridID, refreshJavascriptFunctionName);
            this.Rows            = this.ExecutePagination(rows);
        }
Exemple #2
0
        private IQueryable <T> ExecuteSorting(IQueryable <T> rows, GridStateModel gridState)
        {
            if (gridState.FilterState == null)
            {
                return(rows);
            }

            SortingDirection sortingDirection  = gridState.FilterState.SortingDirection;
            string           sortingColumn     = gridState.FilterState.SortingColumn;
            Type             sortingColumnType = gridState.FilterState.SortingPropertyType;

            if (string.IsNullOrWhiteSpace(sortingColumn) || sortingDirection == SortingDirection.Undefined)
            {
                return(rows);
            }

            // If the source type is Interface, which inherits another Interface, and the property belongs to this base Interface,
            // 'Expression' will throw an error. Then, sorting needs to be done by this base Interface type.
            Type sortingType = this.GetExpressionType(sortingColumn);

            // If the source type is Interface, and if the property, by which sorting is executing, is not declared on the source type,
            // but on the Interfaces that this source Interface inherits, than the sorting must be done in the Interface which declares
            // the property by which sorting is executing
            object sortingRows = this.CastSourceRowsToTheProperExpressionType(sortingType, rows);

            ParameterExpression sortingParameter    = Expression.Parameter(sortingType, "p");
            Expression          parameterExpression = Expression.PropertyOrField(sortingParameter, sortingColumn);
            LambdaExpression    sortingExpression   = Expression.Lambda(parameterExpression, new[] { sortingParameter });
            string sortingMethodName = string.Empty;

            switch (sortingDirection)
            {
            case SortingDirection.Ascending:
                sortingMethodName = "OrderBy";
                break;

            case SortingDirection.Descending:
                sortingMethodName = "OrderByDescending";
                break;
            }

            MethodInfo sortingMethod        = typeof(Queryable).GetMethods().Where(m => m.Name.Equals(sortingMethodName)).Where(m => m.GetParameters().Length == 2).Single();
            MethodInfo genericSortingMethod = sortingMethod.MakeGenericMethod(new[] { sortingType, parameterExpression.Type });

            object sortedRows = genericSortingMethod.Invoke(null, new object[] { rows, sortingExpression });

            return(this.CastRowsToTheSourceType(sortingType, sortedRows));
        }