Exemplo n.º 1
0
        private void Parse(DataTablesRequest requestModel)
        {
            var sortKeyPrefix = requestModel.iSortCol.ToList();
            var columns       = requestModel.sColumns.Split(',').Select(c => {
                if (String.IsNullOrEmpty(c))
                {
                    return(String.Empty);
                }
                else
                {
                    return(c);
                }
            }).ToList();

            dataTablesColumns = new List <DataTablesColumn>();
            var properties = typeof(T).GetProperties();

            columns.ForEach(col => {
                if (string.IsNullOrEmpty(col) == false)
                {
                    int i        = columns.IndexOf(col);
                    var dtColumn = new DataTablesColumn(col, i, false, false)
                    {
                        IsSearchable = requestModel.bSearchable[i],
                        Property     = properties.Where(x => x.Name == col).SingleOrDefault()
                    };

                    dataTablesColumns.Add(dtColumn);
                }
            });

            dataTablesColumns.ForEach(sortable => {
                sortable.IsSortable = requestModel.bSortable[sortable.ColumnIndex];
                sortable.SortOrder  = -1;

                //  Is this item amongst currently sorted columns?
                sortKeyPrefix.ForEach(keyPrefix => {
                    if (sortable.ColumnIndex == Convert.ToInt32(keyPrefix))
                    {
                        int order = sortKeyPrefix.IndexOf(keyPrefix);
                        sortable.IsCurrentlySorted = true;

                        //  Is this the primary sort column or secondary?
                        sortable.SortOrder = order;

                        //  Ascending or Descending?
                        if (requestModel.sSortDir.Count >= order)
                        {
                            sortable.SortDirection = requestModel.sSortDir[order];
                        }
                    }
                });
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets an expression that represents the order linq clause.
        /// </summary>
        /// <typeparam name="T">Generic Type of the object.</typeparam>
        /// <param name="column">Column on which we have to get the order by clause.</param>
        /// <param name="tType">Object type contained into the column.</param>
        /// <returns>An Expression representing the order by LinQ clause.</returns>
        internal static Expression <Func <T, object> > GetOrderByExpression <T>(this DataTablesColumn column, Type tType)
        {
            MemberInfo prop = tType.GetProperty(column.PropertyName);

            if (prop == null)
            {
                return(null);
            }

            ParameterExpression tParam = Expression.Parameter(tType, "x");

            MemberExpression valueInNameProperty = Expression.MakeMemberAccess(tParam, prop);

            UnaryExpression expression = Expression.Convert(
                valueInNameProperty,
                prop.MemberType.Equals(MemberTypes.Property)
                    ? (((PropertyInfo)prop).PropertyType)
                    : typeof(object));

            Expression <Func <T, object> > orderByExpression = Expression.Lambda <Func <T, object> >(expression, tParam);

            return(orderByExpression);
        }