Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use IN keyword in LINQ

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 115
    Comment on it

    In SQL Server, we are very familiar with IN keyword and we use this keyword in many times which is very helpful for us. It save our extra efforts and code line like below query.

     

     

    To achieve this in LINQ , we heve below two mechanism.

    • Using Contain function
    • Using JOIN

     

    1.)  Contain keyword on Array

    Lets take an array EmployesID where we store our employee ID and check column emp.ID IN EmployesID array as below :

     

    var EmployesID = new[] { 1,2,3,4,5 };
    
             (from emp in dbContext.Employes
              join EmpAdd in dbContext.EmpAddress on emp.Id equals EmpAdd.EmployeeId
              where EmployesID.Contains(emp.ID)
              select emp.Name);

     

    2.) JOIN on Array

    Here we join our array with ID as below :

    var EmployesID = new[] { 1,2,3,4,5 };
    
               (from emp in dbContext.Employes
               join abc in EmployesID on emp.ID equals abc
               select emp.Name);

     

    Apply Contain keyword on collection can make impact on performance so here we recommend to use JOIN on array.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: