[英]How To Use C# Linq Predicate with custom Extension Method
I'm trying to write a C# Extension Method, that should take a Predicate and return IQueryable so i can reuse a complicated Where Predicate 我正在尝试编写一个C#扩展方法,它应该采用Predicate并返回IQueryable,这样我就可以重用一个复杂的Where Predicate
Here is what i'm looking at 这是我正在看的
public static IEnumerable<T> AddComplexWhere<T>(this IEnumerable<T> query, DBContext context, Func<T, string> PermissionKeyColumn)
{
return query.Where(pp => context.Permissions
.Where(p => /*PermissionKeyColumn is in Permissions Table p.PermissionKey ??????*/)
.Where(p => true/* another complicated where*/).Any());
}
usage will be 用法将是
context.orders.AddComplexWhere(context,o=>o.permissionKey).ToList()..
this way i can keep reusing the where 这样我就可以继续重复使用
To achieve this you need to use 要实现这一目标,您需要使用
Expression<Func<T, bool>>
Here is example: 这是一个例子:
public static IEnumerable<T> AddComplexWhere<T>(this IEnumerable<T> query, DBContext context, Expression<Func<T, bool>> expression)
{
return query.Where(expression).Any());
}
After this modification your code should just work: 在此修改之后,您的代码应该正常工作:
context.orders.AddComplexWhere(context,o=>o.permissionKey == something).ToList()..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.