快捷搜索:

继LINQ动态组合查询PredicateExtensions讲解

在LINQ动态组合查询中我留下了一个问题便是PredicateExtensions。在这里很简单不必要什么多的根基只要比会And、Or逻辑运算数学常识就够了。

先贴上代码好阐发:

public static class PredicateExtensions

{

public static ExpressionFuncT, bool>> TrueT>() { return f => true; }

public static ExpressionFuncT, bool>> FalseT>() { return f => false; }

public static ExpressionFuncT, bool>> OrT>(this ExpressionFuncT, bool>> expression1,

ExpressionFuncT, bool>> expression2)

{

var invokedExpression = Expression.Invoke(expression2, expression1.Parameters

.CastExpression>());

return Expression.LambdaFuncT, bool>>(Expression.Or(expression1.Body, invokedExpression),

expression1.Parameters);

}

public static ExpressionFuncT, bool>> AndT>(this ExpressionFuncT, bool>> expression1,

ExpressionFuncT, bool>> expression2)

{

var invokedExpression = Expression.Invoke(expression2, expression1.Parameters

.CastExpression>());

return Expression.LambdaFuncT, bool>>(Expression.And(expression1.Body,

invokedExpression), expression1.Parameters);

}

}

您可能还会对下面的文章感兴趣: