Dynamic Lambda expression inside an expression query

Can a dynamic Linq expression be used inside a query expression? Something like that:

from obj1 in ObjectSet1
let res = ObjectSet2.Where(* SomeExpression *)
where ...
select ...


I am trying to build an expression Expression.Lambda<Func<TSource, bool>>

for SomeExpression.

  • Is it possible to use a dynamic Linq expression in an expression query or do I need to rebuild the entire expression tree from scratch?
  • How can I, if any, use obj1 when creating SomeExpression?

Note. I am using Entity Framework, I cannot use SomeExpression.Compile () on Expression Tree.


source to share

2 answers

Not only is this possible, it is normal. Large expression trees can be generated from smaller trees (this is what LINQ does). You only need to transfer Expression<Func<TSource, bool>>

to Where()


You can see how in my answer to this other thread here - replace the operator in the Where Lambda section with a parameter . The interesting part is in the method MakeWhereLambda




If you want it to be fully dynamic, one possible option would be to use serialization serialization:


I started using this a while ago, but this task has been refocused, so I'm not sure how appropriate it is for any given task. However, it looks pretty good ...

Hope it helps.




All Articles