Linq to sql linq到sql组合表达式

Linq to sql linq到sql组合表达式,linq-to-sql,lambda,Linq To Sql,Lambda,有什么方法可以将表达式列表合并为一个?我有一个列表解释程序,并试图合并成一个,然后 Expression<Child, bool> combined = Combine(expList); 试试这个,它是一个构建器,所以你必须在expList中递归地调用它 public Expression<Func<T, bool>> Combine<T>(Expression<Func<T, Boolean>> first, Expr

有什么方法可以将表达式列表合并为一个?我有一个列表解释程序,并试图合并成一个,然后

Expression<Child, bool> combined = Combine(expList);

试试这个,它是一个构建器,所以你必须在expList中递归地调用它

public Expression<Func<T, bool>> Combine<T>(Expression<Func<T, Boolean>> first, Expression<Func<T, Boolean>> second)
{
   var toInvoke = Expression.Invoke(second, first.Parameters);

   return (Expression<Func<T, Boolean>>)Expression.Lambda(Expression.AndAlso(first.Body, toInvoke), first.Parameters);
}

谢谢你的回复。你知道为什么这个例外吗?顺便说一句,上面的例外?二进制?它是由答案中的阻塞还是问题中的阻塞引起的?在问题中-在上面的那一行,唯一的问题是在返回表达式.LambdaExpression.AndAlsofirst.Body的行中,首先调用.Parameters;看起来编译器无法解析T。若我用具体类型替换T,那个么一切都正常。您是否尝试过更改Combine->Combine?可能是我的帖子打错了我刚刚更新了
var result = expList.Cast<Expression>().
Aggregate((p1, p2) => Expression.AndAlso(p1, p2));
{"The binary operator AndAlso is not defined for the types 'System.Func`2[Child,System.Boolean]' and 'System.Func`2[Child,System.Boolean]'."}
public Expression<Func<T, bool>> Combine<T>(Expression<Func<T, Boolean>> first, Expression<Func<T, Boolean>> second)
{
   var toInvoke = Expression.Invoke(second, first.Parameters);

   return (Expression<Func<T, Boolean>>)Expression.Lambda(Expression.AndAlso(first.Body, toInvoke), first.Parameters);
}