none
HOW TO USE 'IN' clause in linq RRS feed

  • Question

  • hi ......

    i am working wiht VS 2010 AND  beginner in LINQ . how to use   SQL 'IN' Clause in Linq Query

     

    Saturday, June 25, 2011 4:51 AM

Answers

  • Hi RageshShiva;

    Using the List object Contains method in the Where clause will create the IN T-SQL clause. The code snippet uses the Microsoft Northwind sample database using the Customers table as an example.

    // This list will be the parameters for the IN clause
    List<string> cities = new List<string>() {"London", "Vancouver", "San Francisco"};
    
    var cutomers = from c in Customers
            where cities.Contains(c.City)
            select c;
    
    
    // The SQL that is sent to the SQL server
    SELECT [t0].[CustomerID], [t0].[CompanyName], 
        [t0].[ContactName], [t0].[ContactTitle], 
        [t0].[Address], [t0].[City], [t0].[Region], 
        [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [Customers] AS [t0]
    WHERE [t0].[City] IN (@p0, @p1, @p2)
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 25, 2011 4:01 PM

All replies

  • Hi RageshShiva;

    Using the List object Contains method in the Where clause will create the IN T-SQL clause. The code snippet uses the Microsoft Northwind sample database using the Customers table as an example.

    // This list will be the parameters for the IN clause
    List<string> cities = new List<string>() {"London", "Vancouver", "San Francisco"};
    
    var cutomers = from c in Customers
            where cities.Contains(c.City)
            select c;
    
    
    // The SQL that is sent to the SQL server
    SELECT [t0].[CustomerID], [t0].[CompanyName], 
        [t0].[ContactName], [t0].[ContactTitle], 
        [t0].[Address], [t0].[City], [t0].[Region], 
        [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [Customers] AS [t0]
    WHERE [t0].[City] IN (@p0, @p1, @p2)
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 25, 2011 4:01 PM
  • Hi Fernando ......

    my problem solved

    Thaks for your valuable replay......

    Saturday, July 2, 2011 5:08 AM
  • Not a problem, glad to help.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 1:39 PM