手动构建 x => 的 linq 表达式x.Child == itemToCompare.Child
我们有一个对象,并且希望基于该对象动态构建 linq 查询。此 linq 语句相当于我们想要构建的内容:
Expression<Func<Sample, bool>> linqExpression
= x => x.Child == itemToCompare.Child;
我们无法完全想出正确的表达式来构建 itemToCompare.Child 部分。这是我们到目前为止所得到的:
var param = Expression.Parameter(typeof(T), "x");
var key = itemToCompare.GetType().GetProperty("Child");
var rhsConstant = Expression.Constant(item);
var innerLambda = Expression.Lambda<Func<T>>(rhsConstant,
new ParameterExpression[0]);
var rhsMemberAccess = Expression.MakeMemberAccess(innerLambda, key);
body = Expression.Equal(lhsPropertyAccess, rhsMemberAccess);
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
我们手工构建的查询的表达式树如下所示:
Lambda Expression: x => (x.Child = value(SampleTests+Sample))
Expression Body: (x.Child = value(SampleTests+Sample))
Parameter 0: 'x', Type: Sample
NodeType: Lambda
Left Lambda Expression: x.Child
Left NodeType: MemberAccess
Lambda Expression: x
Expression Member: Sample Child
NodeType: Parameter
-- a parameter expression
Right Lambda Expression: value(SampleTests+Sample)
Right NodeType: Constant
NodeType: Constant
Value: SampleTests+Sample
实际 lambda 的表达式树如下所示:
Lambda Expression: x => (x.Child = value(SampleTests+<>c__DisplayClass15).itemToCompare.Child)
Expression Body: (x.Child = value(SampleTests+<>c__DisplayClass15).itemToCompare.Child)
Parameter 0: 'x', Type: Sample
NodeType: Lambda
Left Lambda Expression: x.Child
Left NodeType: MemberAccess
Lambda Expression: x
Expression Member: Sample Child
NodeType: Parameter
-- a parameter expression
Right Lambda Expression: value(SampleTests+<>c__DisplayClass15).itemToCompare.Child
Right NodeType: MemberAccess
Lambda Expression: value(SampleTests+<>c__DisplayClass15).itemToCompare
Expression Member: Sample Child
NodeType: MemberAccess
Lambda Expression: value(SampleTests+<>c__DisplayClass15)
Expression Member: Sample itemToCompare
NodeType: Constant
NodeType: Constant
Value: SampleTests+<>c__DisplayClass15
编辑:
我们认为这就是
Expression<Func<Sample, bool>> linqExpression
= x => x.Child == (()=>itemToCompare).Child;
我们试图在表达式中重现的结果。
最终目标是生成一个表达式,其中使用其 Type 的 .Equals() 方法比较 Child 值。
谢谢!
We have an object and we want to build a linq query based on that object on the fly. This linq statement is equivalent to what we want to build:
Expression<Func<Sample, bool>> linqExpression
= x => x.Child == itemToCompare.Child;
We can't quite come up with the right expression to build the itemToCompare.Child part. Here's what we have so far:
var param = Expression.Parameter(typeof(T), "x");
var key = itemToCompare.GetType().GetProperty("Child");
var rhsConstant = Expression.Constant(item);
var innerLambda = Expression.Lambda<Func<T>>(rhsConstant,
new ParameterExpression[0]);
var rhsMemberAccess = Expression.MakeMemberAccess(innerLambda, key);
body = Expression.Equal(lhsPropertyAccess, rhsMemberAccess);
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
The expression tree for our hand-built query looks like:
Lambda Expression: x => (x.Child = value(SampleTests+Sample))
Expression Body: (x.Child = value(SampleTests+Sample))
Parameter 0: 'x', Type: Sample
NodeType: Lambda
Left Lambda Expression: x.Child
Left NodeType: MemberAccess
Lambda Expression: x
Expression Member: Sample Child
NodeType: Parameter
-- a parameter expression
Right Lambda Expression: value(SampleTests+Sample)
Right NodeType: Constant
NodeType: Constant
Value: SampleTests+Sample
The expression tree for the actual lambda looks like:
Lambda Expression: x => (x.Child = value(SampleTests+<>c__DisplayClass15).itemToCompare.Child)
Expression Body: (x.Child = value(SampleTests+<>c__DisplayClass15).itemToCompare.Child)
Parameter 0: 'x', Type: Sample
NodeType: Lambda
Left Lambda Expression: x.Child
Left NodeType: MemberAccess
Lambda Expression: x
Expression Member: Sample Child
NodeType: Parameter
-- a parameter expression
Right Lambda Expression: value(SampleTests+<>c__DisplayClass15).itemToCompare.Child
Right NodeType: MemberAccess
Lambda Expression: value(SampleTests+<>c__DisplayClass15).itemToCompare
Expression Member: Sample Child
NodeType: MemberAccess
Lambda Expression: value(SampleTests+<>c__DisplayClass15)
Expression Member: Sample itemToCompare
NodeType: Constant
NodeType: Constant
Value: SampleTests+<>c__DisplayClass15
Edit:
We think this works out to
Expression<Func<Sample, bool>> linqExpression
= x => x.Child == (()=>itemToCompare).Child;
which is what we're trying to reproduce in our expression.
Ultimately the goal is to produce an Expression where the Child values are compared using their Type's .Equals() method.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对“内部 lambda”感到困惑。只有一个 lambda 表达式:
You're getting confused by the "inner lambda". There's only one lambda expression: