views:

5021

answers:

8

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.

Thanks.

+1  A: 

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
Daren Thomas
+2  A: 
from f in Foo
    where f.FooID ==
     (
      FROM fb in FooBar
      WHERE fb.BarID == 1000
      select fb.FooID

     )
    select f;
Alison
Does this really work? I have troubles believing so...
Daren Thomas
A: 

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
Ngu Soon Hui
+11  A: 

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;
Samuel Jack
Thanks for the link - that was just what I needed.Thanks to everyone else for their answers as well.
Ian Oxley
You might have better performance with constructing a Dictionary with the first query, as the Contains() call in the second query can then be done in O(1) as opposed to O(n).
Daren Thomas
Daren, LINQ to SQL will be transformed to SQL query. Dictionary will be useful when iterating over objects collection.
aku
+17  A: 

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;
aku
Quick and to the point - great answer. Even though building the inner query in a separate statement is definitely clear, might as well know how to do it inline. Thanks!
Jason Bunting
A: 

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

David B
A: 

// create a Dictionary / Set / Collection fids first

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

A: 

// create a Dictionary / Set / Collection fids first

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