如何在自定义扩展方法中使用C#Linq谓词
我正在尝试编写一个C#扩展方法,它应该接受一个谓词并返回IQueryable,这样我就可以重用一个复杂的Where谓词 这就是我所看到的如何在自定义扩展方法中使用C#Linq谓词,c#,.net,entity-framework,linq,C#,.net,Entity Framework,Linq,我正在尝试编写一个C#扩展方法,它应该接受一个谓词并返回IQueryable,这样我就可以重用一个复杂的Where谓词 这就是我所看到的 public static IEnumerable<T> AddComplexWhere<T>(this IEnumerable<T> query, DBContext context, Func<T, string> PermissionKeyColumn) { return q
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());
}
这样,我就可以继续重用where来实现您需要使用的功能
Expression<Func<T, bool>>
您希望扩展方法做什么?附加到where il更新应答检查-那么
query
和context.Permissions
之间的关系是什么。我找不到。你没有在任何地方使用pp
。你能给出一个更完整的例子来说明你想要实现什么吗?这个关系是permissionKey,它在order上,在permission表中,你能在Where子句中使用Func吗?它需要一个predicateit的表达式ofFunc
not Func。谓词本身在Find
函数中使用。但是如何根据添加的权限表中的PermissionKey进行筛选?如果我所做的只是通过我不需要的地方,我可以在哪里使用。我需要它根据PermissionKey列进行筛选。这需要来自AddComplexWhere方法中添加的added Permissions表。它是EF,因此您的对象中可能有Permission类型的属性,对吗?
Expression<Func<T, bool>>
public static IEnumerable<T> AddComplexWhere<T>(this IEnumerable<T> query, DBContext context, Expression<Func<T, bool>> expression)
{
return query.Where(expression).Any());
}
context.orders.AddComplexWhere(context,o=>o.permissionKey == something).ToList()..