コード例 #1
0
ファイル: Repository.cs プロジェクト: Hduc/Project
 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));
 }
コード例 #2
0
ファイル: Repository.cs プロジェクト: Hduc/Project
 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));
 }
コード例 #3
0
ファイル: QueryableExtensions.cs プロジェクト: Hduc/Project
        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);
        }