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
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()..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.