日期:2014-05-17 浏览次数:20775 次
using System.Linq;
using System.Linq.Expressions;
public static class LambdaExpressionExtensions
{
public static Expression<TFunc> And<TFunc>(this Expression<TFunc> expr1, Expression<TFunc> expr2)
{
if (expr1.ReturnType != typeof(bool) || expr2.ReturnType != typeof(bool))
throw new ArgumentException("both lambda expressions must return boolean type");
if (expr1.Parameters.Zip(expr2.Parameters, (p1, p2) => p1.Type == p2.Type).Any(x => x == false))
throw new ArgumentException("expr1 and expr2 must have exactly the same parameters");
var p = expr1.Parameters;
var left = Expression.Invoke(expr1, p);
var right = Expression.Invoke(expr2, p);
var expr = Expression.And(left, right);
return Expression.Lambda<TFunc>(expr, p);
}
}
var left = Expression.Invoke(expr1, p.ToArray());
public static Expression<TFunc> And<TFunc>(this?Expression<TFunc> expr1, Expression<TFunc> expr2)
{
if (expr1.Body.Type != typeof(bool)
------其他解决方案--------------------
更详细请看:
http://bbs.csdn.net/topics/390303038
谢谢
------其他解决方案--------------------
expr2.Body.Type != typeof(bool))
throw new ArgumentException("both?lambda?expressions?must?return?boolean?type");
if (expr1.Parameters.Zip(expr2.Parameters, (p1, p2) => p1.Type == p2.Type).Any(x => x == false))
&n