Use Multiple Where Condition in a LINQ Query

Let us create a console application and see how it is done. Copy the following code in the Console application.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  7. namespace ConsoleApplication3  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.       {  
  13.             string[] cities =  
  14.             {  
  15.                 "MUMBAI","NEW DELHI","AMSTERDAM","ZURICH","PARIS"  
  16.             };  
  18.             var _result = from x in cities  
  19.             where x.StartsWith("M")  
  20.             where x.EndsWith("I")  
  21.             select x;  
  23.             foreach(var city in _result)  
  24.             {  
  25.                 Console.Write("CITY: {0}", city);  
  26.             }  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
In the preceding code we have a string array of cities. We have specified two where conditions in the program. The first where clause checks for the city that has the starting letter  “M” and the second where clause looks for the city that has the ending letter “I”. Clearly we can see there is just one city with starting letter “M” ending with “I”. So we should be getting this as output.

Let us run the program to check whether our code is working fine or not.


Well, we got the desired output. In this way we can use multiple where clauses in a LINQ query.