This post will show you how to generate the " where in " clause in Linq to SQL. Imagine that we want to select rows from the Products table in the Northwind database where ProductId matches 3,4, 10. Our query should include the Where IN clause. Something like this:
SELECT *FROM Products WHERE ProductID in (3,4,10)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Linq_Tips
{
class Program
{
static void Main(string[] args)
{
//Create a list
List<int> list = new List<int>();
//Add items in list
list.AddRange(new int[] {3, 4, 10 });
NorthwindDataContext dc = new NorthwindDataContext();
//Query database
var query = from product in dc.Products
where list.Contains(product.ProductID)
select product;
Console.WriteLine(query);
}
}
}
The output of the above code snippet will look like this: