Linq and Sql "where in" Operator

In this post i will  show you how to generate "where in " clause in Linq to Sql. Imagine that we want to select rows from Products table in 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:

No comments:

Post a Comment