Example #1
0
 public IReadOnlyList <TEntity> Get(
     Expression <Func <TEntity, bool> > predicate,
     IncludingQuery <TEntity> includingQuery = null,
     OrderingQuery <TEntity> orderingQuery   = null,
     PagingQuery pagingQuery = null)
 {
     return(Queryable(predicate, includingQuery, orderingQuery, pagingQuery).ToList());
 }
Example #2
0
 private IQueryable <TEntity> Queryable(
     Expression <Func <TEntity, bool> > predicate,
     IncludingQuery <TEntity> includingQuery = null,
     OrderingQuery <TEntity> orderingQuery   = null,
     PagingQuery pagingQuery = null)
 {
     return(DbSet.Where(predicate).Including(includingQuery).Ordering(orderingQuery).Paging(pagingQuery));
 }
Example #3
0
 public async Task <IReadOnlyList <TEntity> > GetAsync(
     Expression <Func <TEntity, bool> > predicate,
     IncludingQuery <TEntity> includingQuery = null,
     OrderingQuery <TEntity> orderingQuery   = null,
     PagingQuery pagingQuery             = null,
     CancellationToken cancellationToken = default)
 {
     return(await Queryable(predicate, includingQuery, orderingQuery, pagingQuery)
            .ToListAsync(cancellationToken));
 }
Example #4
0
        public static IQueryable <T> Ordering <T>(this IQueryable <T> queryable, OrderingQuery <T> orderingQuery)
        {
            if (orderingQuery == null)
            {
                return(queryable);
            }

            var first = true;

            // Loop through the order column to build the expression tree
            foreach (var column in orderingQuery.Columns)
            {
                // Get the property from the T, based on the key
                var lambda = column.Key;

                // Based on the order direction, get the right method
                string method;
                if (first)
                {
                    method = column.Value == OrderDirection.Ascending ? "OrderBy" : "OrderByDescending";
                    first  = false;
                }
                else
                {
                    method = column.Value == OrderDirection.Ascending ? "ThenBy" : "ThenByDescending";
                }

                // itemType is the type of the TModel
                // exp.Body.Type is the type of the property.
                Type[] types = { typeof(T), lambda.ReturnType };

                // Build the call expression
                var methodCallExpression = Expression.Call(typeof(Queryable), method, types,
                                                           queryable.Expression, lambda);

                // Now you can run the expression against the collection
                queryable = queryable.Provider.CreateQuery <T>(methodCallExpression);
            }

            return(queryable);
        }