0

I am trying to find a way to create the Linq Expression tree for a sub-query in-clause.

SELECT * FROM x WHERE X.Id IN (SELECT XId FROM Y)

Say I have this query, I need a way to create the System.Linq.Expression of the where clause. I have a generic repository base solution for numerous Entities and I am creating the expression tree dynamically for other filters and I would like to add this to the already created expression tree.

I know I can do something like below, but it isn't generic enough.

var xIds = Y.Select(x => x.XId).ToList();
var final = X.Where(x => xIds.Contains(x.Id)).ToList();

But I need Expression tree that this would create.

I played around with this link: Creating a Linq expression dynamically containing a subquery and it doesn't seem to work. I might be missing something though, been staring at this for a while.

Thanks!

Community
  • 1
  • 1
Rodney Pannell
  • 330
  • 5
  • 16
  • What is the problem with the current code? That it executes two queries instead of one? What do you mean it isn't generic enough? What would you like to vary in such query? – Yacoub Massad Aug 08 '16 at 19:14
  • 1
    I'm pretty sure we can help if you provide more practical example of what exactly are you trying to achieve. At least some sort of a method signature (arguments, return type) that are you seeking for. – Ivan Stoev Aug 10 '16 at 19:39

0 Answers0