How do I write SELECT FROM myTable WHERE id IN (SELECT...) in Linq?

10,417

Solution 1

from a in TableA 
where (from b in TableB 
       join c in TableC on b.id equals c.id
       where .. select b.id)
.Contains(a.Id) 
select new { a.Id, a.Name }

Solution 2

There is no out of box support for IN in LINQ. You need to join 2 queries.

Share:
10,417
ScottG
Author by

ScottG

I am a software architect in Detroit, Michigan specializing in e-commerce on the Microsoft stack (ASP.NET MVC, WPF, WCF, SQL Server, and C#). I write software that integrates with Amazon MWS and AWS, Paypal, Google, Jet, and ChannelAdvisor among others.

Updated on June 25, 2022

Comments

  • ScottG
    ScottG almost 2 years

    How do you rewrite this in Linq?

    SELECT Id, Name FROM TableA WHERE TableA.Id IN (SELECT xx from TableB INNER JOIN Table C....)
    

    So in plain english, I want to select Id and Name from TableA where TableA's Id is in a result set from a second query.