Wickasitha's tech Blog

blogging everything

Archive for the ‘Linq’ Category

Using LINQ to Perform “WHERE IN (Value1,Value2)” Queries

  • Wednesday Oct 14,2009 12:06 AM
  • By wickasitha
  • In Linq

I recently needed to select a few products from a database where the product ID matched up with a list of IDs.  That’s easy to do with a normal SQL statement since you can use the “WHERE IN (Value1,Value2)” clause to find what you need.  However, I wanted to do it with LINQ in this case. 

After doing a little research I discovered that executing a query like this using LINQ/Lambdas was actually really easy.  Here’s what I did:

public static Product[] GetProducts(Guid[] prodIDs)
{
    return GetProducts().Where(p => prodIDs.Contains(p.ProductID)).ToArray<Product>();
}
public static Product[] GetProducts(Guid[] prodIDs)
{
   return (from p in GetProducts()
           where prodIDs.Contains(p.ProductID)
           select p).ToArray<Product>();
}

RSS Feeds

QR Code

qr code

QR code created by QR code Widget

Fun with .NET

Visitors

Your Ads Here
Promote your products