How To Use Multiple Where Condition In LINQ

In some situations we are in a position to check two conditions in our logic. So now shall we see how to use the multiple where clause in a linq and lambda query. For that I have created a class and list with dummy values as shown below
  1. public class Farmers {  
  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 double Income {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. List < Farmers > farmerList = new List < Farmers > {  
  20.     new Farmers {  
  21.         ID = 1, Name = "Sekar L", Location = "Krishnagiri", Income = 21000  
  22.     },  
  23.     new Farmers {  
  24.         ID = 2, Name = "Mohan S", Location = "Krishnagiri", Income = 40000  
  25.     },  
  26.     new Farmers {  
  27.         ID = 3, Name = "Sathis S", Location = "Krishnagiri", Income = 18000  
  28.     },  
  29.     new Farmers {  
  30.         ID = 4, Name = "Subash S", Location = "Ooty", Income = 25000  
  31.     },  
  32.     new Farmers {  
  33.         ID = 5, Name = "Robert B", Location = "Banglore", Income = 28000  
  34.     },  
  35. };  
  36. foreach(var farmer in farmerList) {  
  37.     Console.WriteLine("ID : " + farmer.ID + " Name : " + farmer.Name + "Income : " + farmer.Income);  
  38. }  
Result



The above image represents the collection values, now we're going to filter the income between 25,000 and 40,000 using multiple where conditions, we see this in both the linq and lambda query
 
Linq Query 
  1. //Linq  
  2. #region Linq Deffered Query  
  3. var result = from farmer in farmerList  
  4. where farmer.Income > 25000  
  5. where farmer.Income < 40000  
  6. select farmer;  
  7. #endregion  
  8. Console.WriteLine("Linq Query");  
  9. #region Linq Immediate Query Result  
  10. foreach (var farmer in result)  
  11. {  
  12.    Console.WriteLine("ID : " + farmer.ID + " Name : " + farmer.Name + "Income : " + farmer.Income);  
  13. }  
  14. #endregion  
Result

 
 
Lambda Query  
  1. //Lambda  
  2. #region Lambda deffered query  
  3. var result1 = farmerList.Where(a => a.Income > 25000).Where(a => a.Income < 40000);#  
  4. endregion  
  5. Console.WriteLine("Lambda Query");  
  6. #region Lambda Immediate Query Result  
  7. foreach(var farmer in result1) {  
  8.     Console.WriteLine("ID : " + farmer.ID + " Name : " + farmer.Name + "Income : " + farmer.Income);  
  9. }  
  10. #endregion  
Result
 
 
 
Detailed description 
 
We have specified two where conditions in both linq and lambda queries. The first where clause checks for the income that is greater than 25,000 and the second where clause looks for the income that is less than 45,000. We can see there is just one income between 25000 and 40000. So we should be getting this as output from both queries. 

 
 
I hope it's helpful 
Ebook Download
View all
Learn
View all