Beispiel #1
0
        public async Task <QueryResult <T> > GetQueryResultAsync <T>(PostQuery queryObj, Expression <Func <Post, T> > exp)
        {
            queryObj.NotNull();
            exp.NotNull();

            var result = new QueryResult <T>();

            var query = _context.Posts
                        .Include(p => p.Category)
                        .Include(p => p.Tags)
                        .ThenInclude(pt => pt.Tag)
                        .AsQueryable();

            query = query
                    .ApplyFiltering(queryObj)
                    .ApplySearching(queryObj);

            result.TotalItems = await query.CountAsync();

            query = query
                    .ApplyOrdering(queryObj)
                    .ApplyPaging(queryObj);

            result.Items = await query
                           .Select(exp)
                           .ToListAsync();

            return(result);
        }
Beispiel #2
0
        public static IQueryable <Post> ApplyFiltering(this IQueryable <Post> query, PostQuery queryObj)
        {
            query.NotNull();
            queryObj.NotNull();

            if (queryObj.CategoryId.HasValue)
            {
                query = query.Where(p => p.CategoryId == queryObj.CategoryId.Value);
            }

            if (queryObj.TagId.HasValue)
            {
                query = query.Where(p => p.Tags.Any(pt => pt.TagId == queryObj.TagId.Value));
            }

            return(query);
        }