Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 如何将此Lambda转换为表达式_C#_Linq_Expression_Predicate - Fatal编程技术网

C# 如何将此Lambda转换为表达式

C# 如何将此Lambda转换为表达式,c#,linq,expression,predicate,C#,Linq,Expression,Predicate,如何将具有不同签名的Func转换为具有不同签名的表达式: 当转换中有自定义逻辑且语法不完全相同时,如何转换 Expression<Func<T, bool>) Convert(Func<T, List<TJ>, bool> expr2) { // the returning bool would be the execution result of expr2(t, list) where t is the T and list is of ty

如何将具有不同签名的Func转换为具有不同签名的表达式:

当转换中有自定义逻辑且语法不完全相同时,如何转换

Expression<Func<T, bool>) Convert(Func<T, List<TJ>, bool> expr2)
{
    // the returning bool would be the execution result of expr2(t, list) where t is the T and list is of type List<TJ>

    // the returning T would be the same T

    return Expression.Lambda<Func<T, bool>>
                  (???);
}

Expression无法在运行时将
Func
转换为
表达式。这项艰巨的工作是由编译器为您完成的。(你当然可以制造
表达式
,但你必须自己编写所有逻辑。)不,你当然可以,并演示了如何。问题是当转换中有自定义逻辑且语法不完全相同时如何转换。谢谢,我没听你的。实际上,你的最后一句话是问如何做。我是说“下面的方法”是不可能写的。你以为我指的是别的吗?我怀疑您不知道
Func
表达式的“转换”发生在编译时。
internal static Expression<Func<T, bool>> OrForEachBatch<T, TJ>(this Expression<Func<T, bool>> expr1, List<List<TJ>> batches, Func<T, List<TJ>, bool> expr2)
{
    for (int index = 0; index < batches.Count; index++)
    {
        List<TJ> eventIdBatch = batches[index];

        Expression<Func<T, bool>> converted = Convert(expr2);

        expr1 = expr1.Or(converted);
    }

    return expr1;
}
 var batches = new List<List<int>>();
 var predicate = PredicateBuilder
                    .False<Event>()
                    .Or(e => e.ModifiedDate >= modifiedSince.Value)
                    .OrForEachBatch(batches, (e, batch) => batch.Contains(e.Id));
private static Expression<Func<T, bool>> FuncToExpression<T>(Func<T, bool> f)  
{  
    return x => f(x);  
}