LINQ式ツリーによるクエリー条件の動的構築
6896 ワード
1つ目の方法:
2つ目の方法:
私が実際に応用したところ、第1の方法PredicateExtensionsクラスにはDbDataContextでのみ使用するのに適しているという欠陥があり、EntityContextで使用すると、LINQ to EntitiesはLINQ式ノードタイプ「Invoke」をサポートしていないとエラーが発生します.第2の方法を使ってこの问题があることはできなくて、问题の根源について私はまだ原因を探し当てていないで、また达人の指导を望んで、ありがとうございます!
public static class PredicateExtensions
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1,
Expression<Func<T, bool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression),
expression1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
Expression<Func<T, bool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body,
invokedExpression), expression1.Parameters);
}
}
2つ目の方法:
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
// build parameter map (from parameters of second to parameters of first)
var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
// replace parameters in the second lambda expression with parameters from the first
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
// apply composition of lambda expression bodies to parameters from the first expression
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.And);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.Or);
}
}
私が実際に応用したところ、第1の方法PredicateExtensionsクラスにはDbDataContextでのみ使用するのに適しているという欠陥があり、EntityContextで使用すると、LINQ to EntitiesはLINQ式ノードタイプ「Invoke」をサポートしていないとエラーが発生します.第2の方法を使ってこの问题があることはできなくて、问题の根源について私はまだ原因を探し当てていないで、また达人の指导を望んで、ありがとうございます!