How To Use InClause In LINQ And Lambda

Before moving to this InClause concept let's create a model and add the dummy values to it,
  1. public class Employee {  
  2.     public int ID {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Location {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public decimal Income {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. List < Employee > employees = new List < Employee > {  
  20.     new Employee {  
  21.         ID = 1, Name = "Gnanavel Sekar", Location = "India", Income = 1500000  
  22.     },  
  23.     new Employee {  
  24.         ID = 2, Name = "Robert A", Location = "India", Income = 14000000  
  25.     },  
  26.     new Employee {  
  27.         ID = 3, Name = "Gokul", Location = "UK", Income = 18000000  
  28.     },  
  29.     new Employee {  
  30.         ID = 4, Name = "Karthik", Location = "USA", Income = 1250000  
  31.     },  
  32.     new Employee {  
  33.         ID = 5, Name = "Subash S", Location = "India", Income = 1280000  
  34.     },  
  35. };  
  36. foreach(var employee in employees) {  
  37.     Console.WriteLine("ID : " + employee.ID + " Name : " + employee.Name + " Income : " + employee.Income);  
  38. }  
Run your application,
 
How To Use InClause In LINQ And Lambda
 
Okay! Let's come to the concept. If we want to apply more than one condition we can do so with where condition, but if want to determine whether the specified value matches any value in a list for a particular column it means we have to use the InClause.
 
The above image represents the collection of the employee. Now we're going to filter for locations India and UK using InClause. We will see this in both the LINQ and lambda query.
 
Linq query
 
Let's take an example, I wanted to search the employees whose location are India and UK. For that, I have created one array in the name of myInClause which holds the locations to filter.
  1. var myInClause = new string[] { "India""UK"};  
  2. var LinqResult = from emp in employees  
  3. where myInClause.Contains(emp.Location)  
  4. select emp;  
  5. foreach (var employee in LinqResult)  
  6. {  
  7.    Console.WriteLine("ID : " + employee.ID + " Name : " + employee.Name + " Income : " + employee.Income);  
  8. }  
Result,
 
How To Use InClause In LINQ And Lambda 
 
Lambda Query
  1. Console.WriteLine("Lamda");  
  2. Console.WriteLine("***************");  
  3. var results = employees.Where(x => myInClause.Contains(x.Location));  
  4. foreach (var employee in LinqResult)  
  5. {  
  6.    Console.WriteLine("ID : " + employee.ID + " Name : " + employee.Name + " Location :" + employee.Location + " Income : " + employee.Income);  
  7. }  
Result 
 
How To Use InClause In LINQ And Lambda 
 
We have specified locations as India and UK in both LINQ and lambda queries. We can see there are just three employees from India and UK. So we should be getting the above output from both queries. 
 
Try your own for the below list,
  1. var myInClauseInt = new int[] { 4, 5 };   
I hope it's helpful.