SQL 'IN' statement in LINQ for objects

I am having trouble creating a LINQ query with parameters IN.
Corresponding SQL query

SELECT * FROM TABLEDEMO WHERE ID IN(SELECT ID FROM TABLE2)

How to achieve the same result using LINQ?
I can also take a list variable to store multiple identifiers.

 (from x in objEntity.TABLEDEMO
 where x.TABLEDEMO (here should be the in parameter)
 select x);
+3
source share
3 answers

You need to use Contains:

from x in objEntity.TABLEDEMO
where objEntity.Table2.Contains(y => y.ID == x.ID)
select x;

You cannot write this different, i.e. There is no query style statement that you can use.

+3
source
from x in objEntity.Tabledemo
where (from y in objEntity.table2
       select ID).contains(x.ID)
select x
+3
source

Use the operator Any:

from x in objEntity.TABLEDEMO
where otherQuery.Any(oq => oq == x.ID)
select x
+1
source

All Articles