C# 过滤和分页在.NET Core 2.1 EF中不起作用
我尝试对GET查询应用筛选器和分页。事件当筛选器和分页不为null且代码到达IF内时,查询始终返回所有类别。有什么问题吗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 =>
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,则它根本不会影响类别
对象。