本文介绍了合并两个LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有是在不同的时间建成了两个表达式,但需要为了得到一个准确的where子句中的分组合并。我曾尝试选项,但我使用实体框架和它doesn'不懂的调用
功能。我见过一些在 ExpressionVisitor
替代的,但我不认为我有什么,我需要做好足够的了解。
如果任何人都可以请点我在正确的方向我将十分感激,那感觉就像是接近那里。
Where子句1A (对象类型表达式来; Func键< T,BOOL>>
)的
{PARM => parm.Name.Contains(帽子)}
Where子句1B 的(对象类型 LambdaExpression
,但可以使用(表达式来; Func键< T,BOOL>>)LambdaExpression
)的
{PARM => parm.Attributes.Any(PARM =>((parm.Name ==测试)AndAlso运算(parm.Value ==21)))}
需要的地方条款
{PARM = GT;
parm.Name.Contains(帽子)(安培;&安培; / ||)
parm.Attributes.Any(PARM =>((parm.Name ==测试)AndAlso运算( parm.Value ==21)))
}
如果有人可以请帮助我合并的 Where子句1A 和 Where子句1B ,我会非常感激。
只是一个供参考在哪里()和任何()子句从IQueryable的,不知道如果该事项获得泛型方法
Func键< MethodInfo的,布尔> methodLambda = M => m.Name ==任何&放大器;&安培; m.GetParameters()==长度2。
MethodInfo的方法= typeof运算(可查询).GetMethods(),其中(methodLambda)。单()MakeGenericMethod(ActualQueryableType)。
ParameterExpression parentMember = Expression.Parameter(typeof运算(T),parentParm);
//创建任何()的表达式树并将其转换为拉姆达
MethodCallExpression callAny = Expression.Call(方法,成员,EXP);
LambdaExpression lambdaAny = Expression.Lambda(callAny,参数);
VAR combExp = parentExp.And((表达式来; Func键< T,BOOL>>)lambdaAny);
MethodCallExpression whereCall = Expression.Call(typeof运算(可查询),去哪儿,新类型[] {} query.ElementType,新的表达式[] {
query.Expression ,
Expression.Quote(combExp)
});
=查询(IQueryable的< T>)query.Provider.CreateQuery(whereCall);
使用,当我得到的错误调用
是
解决方案
Here's an implementation of PredicateBuilder
that doesn't use Invoke
:
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<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, secondBody), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso(expr1.Body, secondBody), expr1.Parameters);
}
}
It instead uses a Replace
method (implementation below) that replaces all instances of one expression with another.
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
Using this you can now use And
to AND together two predicate expressions that take the same input.
这篇关于合并两个LINQ表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!