Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

LINQ and "WHERE id IN (...)" clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oleg Ogurok - 13 Oct 2007 21:57 GMT
Hi there,

Is there an equivalent of "WHERE .. IN (...)" clause in LINQ when
querying a SQL database?

E.g. I have list of IDs and I need to get the corresponding table
records.

Thanks,
-Oleg.
Frans Bouma [C# MVP] - 14 Oct 2007 11:01 GMT
> Hi there,
>
[quoted text clipped - 3 lines]
> E.g. I have list of IDs and I need to get the corresponding table
> records.

List<string> myIDs = new List<string>() { "CHOPS", "BLONP"};
NorthwindContext nw = new NorthwindContext();
var q = from o in nw.Orders
    where myIDs.Contains(o.CustomerID)
    select o;

    Yes, this is backwards, but it's how the Linq designers thought
everyone should use databases these days.

    I have no idea why there's no such thing like:
var q = from o in nw.Orders
    where o.CustomerID in myIDs
    select o;

    I mean, would that have been so incredibly bad? No it would have been
better IMHO because it would be more natural for people who know SQL.

    Now, for the 1000$ question, how to do a query like:
SELECT * FROM Orders
WHERE CustomerID IN
(
    SELECT CustomerID FROM Customers WHERE Country = @country
)

    if you don't have the list in-memory?
    I have no idea.

    Btw, the 'Contains' is an extension method on list and handled by the
Linq provider to produce SQL. It thus depends on the Linq provider if
this results in an IN() query.

    Next week: how to do a left join in linq. No, not with 'left join'. :P

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.