C# 如何将此Lambda转换为表达式
如何将具有不同签名的Func转换为具有不同签名的表达式: 当转换中有自定义逻辑且语法不完全相同时,如何转换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
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);
}