Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 过滤和分页在.NET Core 2.1 EF中不起作用_C#_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 过滤和分页在.NET Core 2.1 EF中不起作用

C# 过滤和分页在.NET Core 2.1 EF中不起作用,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我尝试对GET查询应用筛选器和分页。事件当筛选器和分页不为null且代码到达IF内时,查询始终返回所有类别。有什么问题吗 public async Task<List<Category>> GetListAsync(CategoryListFilter filter = null, Paging paging = null) { var categories = _context.Categories .Include(category =>

我尝试对GET查询应用筛选器和分页。事件当筛选器和分页不为null且代码到达IF内时,查询始终返回所有类别。有什么问题吗

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();
}
公共异步任务GetListAsync(CategoryListFilter=null,分页=null) { var categories=\u context.categories .Include(category=>category.Image) .Include(category=>category.Parent) .Include(category=>category.CategoryProperties) .ThenClude(property=>property.property) .ThenClude(属性=>property.ValueType) .然后包括(valueType=>valueType.InputType); if(filter!=null&&!String.IsNullOrEmpty(filter.SearchText)) { 类别 .Where(category=>category.Title.Contains(filter.SearchText)); } if(分页!=null) { 类别 .Skip(分页.Offset) .Take(分页限制); } 返回等待类别。toListSync(); } 你肯定错过了

categories = categories
            .Skip(paging.Offset)
            .Take(paging.Limit);
如果没有它,您将同时调用这两个函数,但会放弃结果

这同样适用于
Where
子句

(注意,这可能需要为
类别
提供一个显式类型,以便赋值的左侧和右侧都属于
IQueryable

类别。如果(…)
返回一个queryable,则它根本不会影响
类别
对象。