我正在尝试创建扩展方法来查询 ef core。我有这个扩展:
public static class FiltrExtension
{
public static IQueryable<Contract> Filtr(this IQueryable<Contract> query, OrderFilter filter)
{
if (!string.IsNullOrWhiteSpace(filter.OrderType))
query.Where(x => x.OrderType == filter.OrderType);
return query;
}
}
然后我尝试过滤数据,但扩展方法什么也不做:
var data = await _uow.DatabaseContext.Contract.Filtr(filter).ToListAsync();
有人可以帮忙吗?
最佳答案
您应该将 query.Where(x => x.OrderType == filter.OrderType);
的结果放入 query
中,如下所示
public static class FiltrExtension
{
public static IQueryable<Contract> Filtr(this IQueryable<Contract> query, OrderFilter filter)
{
if (!string.IsNullOrWhiteSpace(filter.OrderType))
query = query.Where(x => x.OrderType == filter.OrderType);
return query;
}
}
关于c# - EF Core 3 使用扩展方法过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031224/