[英]EF Core 3 filtering data with Extension method
I am trying to make extension method for querying ef core.我正在尝试制作用于查询 ef 核心的扩展方法。 I have this Extension:我有这个扩展:
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;
}
}
and then I am trying to filtr data, but the extension method doing nothing:然后我试图过滤数据,但扩展方法什么也没做:
var data = await _uow.DatabaseContext.Contract.Filtr(filter).ToListAsync();
Can anybody help please?有人可以帮忙吗?
You should put the result of query.Where(x => x.OrderType == filter.OrderType);
你应该把query.Where(x => x.OrderType == filter.OrderType);
的结果放在into query
like this进入这样的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;
}
}
You have to return the results:您必须返回结果:
public static class FiltrExtension
{
public static IQueryable<Contract> Filtr(this IQueryable<Contract> query, OrderFilter filter)
{
if (!string.IsNullOrWhiteSpace(filter.OrderType))
var result = query.Where(x => x.OrderType == filter.OrderType);
return result;
}
}
You can user regular expressions:您可以使用正则表达式:
public static IQueryable<Contract> And<Contract>(this IQueryable<Contract> source, Expression<Func<Contract, bool>> predicate)
{
return source.Where(predicate);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.