A filtering operator in LINQ specifies the statement should only affect rows that meet specified criteria. The criteria expressed as predicates, the where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ.
In this tutorial we’ll learn:
- What is Where Clause in LINQ?
- How to use Where Operator for filtering data
- Linq Where Clause Programming Example with C#
The Where Operator is used in query expression for filtering result based on your condition. This query expression matches each result with your condition set and return only the rows which matches the where condition. You can apply multiple filter in a single where condition by using other linq operator.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class Program { static void Main(string[] args) { //Creating List IList<string> productList = new List<string>() { "Hard Disk", "Monitor", "SSD Disk", "RAM", "Processor", "Bluetooth", "Keyboard & Mouse" }; //Filtering Data var result = from s in productList where s.Contains("Disk") select s; //Printing Result foreach(string str in result) { Console.WriteLine(str.ToString()); } Console.ReadKey(); } } } |
Output:
1 2 3 4 |
Hard Disk SSD Disk |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } } class Program { static void Main(string[] args) { // Creating List IList<ProductStore> productList = new List<ProductStore>(); productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 }); productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 }); productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 }); productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 }); productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 }); productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 }); //LINQ Query Syntax var result = from p in productList where p.productPrice > 2000 && p.productPrice < 5000 select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.Select(p => p); foreach (var list in result) { Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice); } Console.ReadKey(); } } } |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; using System.Linq; namespace Linqtutorials { class Program { static void Main(string[] args) { string[] countries = { "India", "Australia", "USA", "Argentina", "Peru", "China" }; IEnumerable<string> result = countries.Where(x => x.StartsWith("A")); foreach (var country in result) { Console.WriteLine(country); } Console.ReadLine(); } } } |
Output:
1 2 3 4 |
Australia Argentina |