How can you handle an IN sub-query with LINQ to SQL?

SqlLinqLinq to-Sql

Sql Problem Overview


I'm a bit stuck on this. Basically I want to do something like the following SQL query in LINQ to SQL:

SELECT f.* 
FROM Foo f
WHERE f.FooId IN (
    SELECT fb.FooId
    FROM FooBar fb
    WHERE fb.BarId = 1000
)

Any help would be gratefully received.

Sql Solutions


Solution 1 - Sql

General way to implement IN in LINQ to SQL

var q = from t1 in table1
        let t2s = from t2 in table2
                  where <Conditions for table2>
                  select t2.KeyField
        where t2s.Contains(t1.KeyField)
        select t1;

General way to implement EXISTS in LINQ to SQL

var q = from t1 in table1
        let t2s = from t2 in table2
                  where <Conditions for table2>
                  select t2.KeyField
        where t2s.Any(t1.KeyField)
        select t1;

Solution 2 - Sql

Have a look at this article. Basically, if you want to get the equivalent of IN, you need to construct an inner query first, and then use the Contains() method. Here's my attempt at translating:

var innerQuery = from fb in FoorBar where fb.BarId = 1000 select fb.FooId;
var result = from f in Foo where innerQuery.Contains(f.FooId) select f;

Solution 3 - Sql

from f in Foo
	where f.FooID ==
		(
			FROM fb in FooBar
			WHERE fb.BarID == 1000
			select fb.FooID
			
		)
	select f;

Solution 4 - Sql

Try using two separate steps:

// create a Dictionary / Set / Collection fids first
var fids = (from fb in FooBar
            where fb.BarID = 1000
            select new { fooID = fb.FooID, barID = fb.BarID })
            .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo
where fids.HasKey(f.FooId)
select f

Solution 5 - Sql

// create a Dictionary / Set / Collection fids first

http://www.doyouknow.in/How.aspx">Find Other Artilces

var fids = (from fb in FooBar
            where fb.BarID = 1000
            select new { fooID = fb.FooID, barID = fb.BarID })
            .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo
where fids.HasKey(f.FooId)
select f

Solution 6 - Sql

Try this

var fooids = from fb in foobar where fb.BarId=1000 select fb.fooID
var ff = from f in foo where f.FooID = fooids select f

Solution 7 - Sql

var foos = Foo.Where<br>
( f => FooBar.Where(fb.BarId == 1000).Select(fb => fb.FooId).Contains(f.FooId));

Solution 8 - Sql

// create a Dictionary / Set / Collection fids first

http://www.doyouknow.in/DoYouKnow/How.aspx">Find Other Artilces

var fids = (from fb in FooBar where fb.BarID = 1000 select new { fooID = fb.FooID, barID = fb.BarID }) .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo where fids.HasKey(f.FooId) select f

Solution 9 - Sql

from f in foo
where f.FooID equals model.FooBar.SingleOrDefault(fBar => fBar.barID = 1000).FooID
select new
{
f.Columns
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan OxleyView Question on Stackoverflow
Solution 1 - SqlakuView Answer on Stackoverflow
Solution 2 - SqlSamuel JackView Answer on Stackoverflow
Solution 3 - SqlNakedBrunchView Answer on Stackoverflow
Solution 4 - SqlDaren ThomasView Answer on Stackoverflow
Solution 5 - SqlJimit ShahView Answer on Stackoverflow
Solution 6 - SqlGravitonView Answer on Stackoverflow
Solution 7 - SqlAmy BView Answer on Stackoverflow
Solution 8 - SqlJimit ShahView Answer on Stackoverflow
Solution 9 - SqlMox ShahView Answer on Stackoverflow