简体   繁体   English

筛选和分页在.NET Core 2.1 EF中不起作用

[英]Filtering and paging not working in .NET Core 2.1 EF

I try to apply filter and pagination to my GET query. 我尝试将过滤器和分页应用于我的GET查询。 Event when filter and pagination is not null and code reach inside the IF's, query always return all categories. 当filter和pagination不为null并且代码到达IF内部时发生的事件,查询始终返回所有类别。 What's the problem? 有什么问题?

public async Task<List<Category>> GetListAsync(CategoryListFilter filter = null, Paging paging = null)
{
    var categories = _context.Categories
        .Include(category => category.Image)
        .Include(category => category.Parent)
        .Include(category => category.CategoryProperties)
            .ThenInclude(property => property.Property)
                .ThenInclude(property => property.ValueType)
                    .ThenInclude(valueType => valueType.InputType);

    if (filter != null && !String.IsNullOrEmpty(filter.SearchText))
    {
        categories
            .Where(category => category.Title.Contains(filter.SearchText));
    }

    if(paging != null)
    {
        categories
            .Skip(paging.Offset)
            .Take(paging.Limit);
    }

    return await categories.ToListAsync();
}

You definitely miss 你肯定想念

categories = categories
            .Skip(paging.Offset)
            .Take(paging.Limit);

Without it, you call both but discard the result. 没有它,您将两者都调用,但会丢弃结果。

The same applies to the Where clause. 这同样适用于Where子句。

(note that this possibly requires providing an explicit type for categories so that both left and right side of the assignment are of the IQueryable<Category> ) (请注意,这可能需要提供categories的显式类型,以便分配的左侧和右侧都属于IQueryable<Category>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM