manually build linq expression for x => x.Child == itemToCompare.Child

后端 未结 1 776
花落未央
花落未央 2021-01-07 07:28

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&l         


        
相关标签:
1条回答
  • 2021-01-07 08:11

    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);
    
    0 讨论(0)
提交回复
热议问题