<p>I'm a bit stuck on this. Basically I want to do something like the following SQL query in LINQ to SQL:</p>
<pre><code>SELECT f.* 
FROM Foo f
WHERE f.FooId IN (
    SELECT fb.FooId
    FROM FooBar fb
    WHERE fb.BarId = 1000
)
</code></pre>
<p>Any help would be gratefully received.</p>

[{'answer_id': 51345, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 2, 'selected': False, 'text': '<p>Try using two separate steps:</p>\n\n<pre><code>// create a Dictionary / Set / Collection fids first\nvar fids = (from fb in FooBar\n            where fb.BarID = 1000\n            select new { fooID = fb.FooID, barID = fb.BarID })\n            .ToDictionary(x =&gt; x.fooID, x =&gt; x.barID);\n\nfrom f in Foo\nwhere fids.HasKey(f.FooId)\nselect f\n</code></pre>\n'}, {'answer_id': 51346, 'author': 'NakedBrunch', 'author_id': 3742, 'author_profile': 'https://Stackoverflow.com/users/3742', 'pm_score': 2, 'selected': False, 'text': '<pre><code>from f in Foo\n    where f.FooID ==\n        (\n            FROM fb in FooBar\n            WHERE fb.BarID == 1000\n            select fb.FooID\n\n        )\n    select f;\n</code></pre>\n'}, {'answer_id': 51354, 'author': 'Graviton', 'author_id': 3834, 'author_profile': 'https://Stackoverflow.com/users/3834', 'pm_score': 0, 'selected': False, 'text': '<p>Try this</p>\n\n<pre><code>var fooids = from fb in foobar where fb.BarId=1000 select fb.fooID\nvar ff = from f in foo where f.FooID = fooids select f\n</code></pre>\n'}, {'answer_id': 51400, 'author': 'Samuel Jack', 'author_id': 1727, 'author_profile': 'https://Stackoverflow.com/users/1727', 'pm_score': 7, 'selected': True, 'text': '<p>Have a look at <a href="http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql" rel="noreferrer">this article</a>. 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:</p>\n\n<pre><code>var innerQuery = from fb in FoorBar where fb.BarId = 1000 select fb.FooId;\nvar result = from f in Foo where innerQuery.Contains(f.FooId) select f;</code></pre>\n'}, {'answer_id': 51417, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 6, 'selected': False, 'text': '<h1>General way to implement IN in LINQ to SQL</h1>\n\n<pre><code>var q = from t1 in table1\n        let t2s = from t2 in table2\n                  where &lt;Conditions for table2&gt;\n                  select t2.KeyField\n        where t2s.Contains(t1.KeyField)\n        select t1;\n</code></pre>\n\n<h1>General way to implement EXISTS in LINQ to SQL</h1>\n\n<pre><code>var q = from t1 in table1\n        let t2s = from t2 in table2\n                  where &lt;Conditions for table2&gt;\n                  select t2.KeyField\n        where t2s.Any(t1.KeyField)\n        select t1;\n</code></pre>\n'}, {'answer_id': 65218, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 0, 'selected': False, 'text': '<pre><code>var foos = Foo.Where&lt;br&gt;\n( f =&gt; FooBar.Where(fb.BarId == 1000).Select(fb =&gt; fb.FooId).Contains(f.FooId));\n</code></pre>\n'}, {'answer_id': 1088069, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>// create a Dictionary / Set / Collection fids first</p>\n\n<p><a href="http://www.doyouknow.in/How.aspx" rel="nofollow noreferrer">Find Other Artilces</a></p>\n\n<pre><code>var fids = (from fb in FooBar\n            where fb.BarID = 1000\n            select new { fooID = fb.FooID, barID = fb.BarID })\n            .ToDictionary(x =&gt; x.fooID, x =&gt; x.barID);\n\nfrom f in Foo\nwhere fids.HasKey(f.FooId)\nselect f\n</code></pre>\n'}, {'answer_id': 1088178, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>// create a Dictionary / Set / Collection fids first</p>\n\n<p><a href="http://www.doyouknow.in/DoYouKnow/How.aspx" rel="nofollow noreferrer">Find Other Artilces</a></p>\n\n<pre><code>var fids = (from fb in FooBar where fb.BarID = 1000 select new { fooID = fb.FooID, barID = fb.BarID }) .ToDictionary(x =&gt; x.fooID, x =&gt; x.barID);\n\nfrom f in Foo where fids.HasKey(f.FooId) select f\n</code></pre>\n'}, {'answer_id': 8968391, 'author': 'Mox Shah', 'author_id': 1107638, 'author_profile': 'https://Stackoverflow.com/users/1107638', 'pm_score': 0, 'selected': False, 'text': '<pre><code>from f in foo\nwhere f.FooID equals model.FooBar.SingleOrDefault(fBar =&gt; fBar.barID = 1000).FooID\nselect new\n{\nf.Columns\n};\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51339', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1904/']