Exemple #1
0
        private static IQueryable <TSource> OrderBy <TSource>(this IQueryable <TSource> query, string propertyName, OrderDirection direction)
        {
            var entity       = typeof(TSource);
            var propertyInfo = entity.GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
            var arg          = Expression.Parameter(entity, "x");
            var property     = Expression.Property(arg, propertyName);
            var selector     = Expression.Lambda(property, arg);

            var actionName = direction.Equals(OrderDirection.Asc)
                ? "OrderBy"
                : "OrderByDescending";

            var method = typeof(Queryable).GetMethods()
                         .Where(m => m.Name == actionName && m.IsGenericMethodDefinition)
                         .Single(m =>
            {
                var parameters = m.GetParameters().ToList();
                return(parameters.Count == 2);
            });

            if (propertyInfo == null)
            {
                return(query);
            }

            var genericMethod = method.MakeGenericMethod(entity, propertyInfo.PropertyType);

            var newQuery = (IOrderedQueryable <TSource>)genericMethod.Invoke(genericMethod, new object[] { query, selector });

            return(newQuery);
        }
        public IQueryable <T> GetAllAsQuery(Expression <Func <T, object> > orderby = null, OrderDirection orderDirection = OrderDirection.Asc, params Expression <Func <T, object> >[] includes)
        {
            IQueryable <T> query = _context.Set <T>();

            if (orderby != null)
            {
                query = orderDirection.Equals(OrderDirection.Asc) ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
            }

            if (includes != null)
            {
                foreach (var i in includes)
                {
                    query = query.Include(i);
                }
            }

            return(query);
        }
Exemple #3
0
        public static IQueryable <T> OrderBy <T>(this IQueryable <T> source, string property, OrderDirection direction) where T : class
        {
            //STEP 1: Verify the property is valid
            var searchProperty = typeof(T).GetProperty(property);

            if (searchProperty == null)
            {
                throw new ArgumentException("property");
            }

            if (!searchProperty.PropertyType.IsValueType &&
                !searchProperty.PropertyType.IsPrimitive &&
                !searchProperty.PropertyType.Namespace.StartsWith("System") &&
                !searchProperty.PropertyType.IsEnum)
            {
                throw new ArgumentException("property");
            }

            if (searchProperty.GetMethod == null ||
                !searchProperty.GetMethod.IsPublic)
            {
                throw new ArgumentException("property");
            }

            //STEP 2: Create the OrderBy property selector
            var parameter    = Expression.Parameter(typeof(T), "o");
            var selectorExpr = Expression.Lambda(
                Expression.Property(parameter, property),
                parameter);

            //STEP 3: Update the IQueryable expression to include OrderBy
            Expression queryExpr = source.Expression;

            queryExpr = Expression.Call(typeof(Queryable),
                                        direction.Equals(OrderDirection.Asc) ? "OrderBy" : "OrderByDescending",
                                        new Type[] { source.ElementType, searchProperty.PropertyType },
                                        queryExpr,
                                        selectorExpr);

            return(source.Provider.CreateQuery <T>(queryExpr));
        }