Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在自定义扩展方法中使用C#Linq谓词_C#_.net_Entity Framework_Linq - Fatal编程技术网

如何在自定义扩展方法中使用C#Linq谓词

如何在自定义扩展方法中使用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

我正在尝试编写一个C#扩展方法,它应该接受一个谓词并返回IQueryable,这样我就可以重用一个复杂的Where谓词

这就是我所看到的

    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的
表达式
of
Func
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()..