手动构建 x => 的 linq 表达式x.Child == itemToCompare.Child

发布于 2024-09-28 09:25:40 字数 2583 浏览 0 评论 0原文

我们有一个对象,并且希望基于该对象动态构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2024-10-05 09:25:40

您对“内部 lambda”感到困惑。只有一个 lambda 表达式:

var param = Expression.Parameter(typeof(Sample), "x");
var key = itemToCompare.GetType().GetProperty("Child");
var rhs = Expression.MakeMemberAccess(Expression.Constant(itemToCompare), key);
var lhs = Expression.MakeMemberAccess(param, key);
var body = Expression.Equal(lhs, rhs);
var lambda = Expression.Lambda<Func<Sample, bool>>(body, param);

You're getting confused by the "inner lambda". There's only one lambda expression:

var param = Expression.Parameter(typeof(Sample), "x");
var key = itemToCompare.GetType().GetProperty("Child");
var rhs = Expression.MakeMemberAccess(Expression.Constant(itemToCompare), key);
var lhs = Expression.MakeMemberAccess(param, key);
var body = Expression.Equal(lhs, rhs);
var lambda = Expression.Lambda<Func<Sample, bool>>(body, param);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文