Showing posts with label Collection. Show all posts
Showing posts with label Collection. Show all posts

Thursday, March 9, 2017

Linq to SQL where clause against collection of list values

Problem:

I was working on an EF Model with Linq 2 SQL.  
I sometimes depend on the SQL sp's which depend upon subqueries

select x.value1,x.value2 from myTable x
where
x.primarykey in (select foreignkey from myTable2 where name="jump")

How do you accomplish this in Linq?


Solution:

var innerquery = (from iq in myTable2
where iq.name.Equals("jump")
select iq.id);

var Ids = innerquery.ToList();

var query = 
from mt in myTable
where
Ids.Any(qq => mt.Id.Equals(qq)
select mt);

Source:

http://stackoverflow.com/questions/1075540/linq-to-sql-how-to-do-where-column-in-list-of-values