LINQ – Where Clause with Programming Example

In this tutorial, you will learn:
  1. What is Where Clause in LINQ?
  2. How to use Where Operator for filtering data
  3. 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.

Programming Example 1:

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:


Hard Disk
SSD Disk
_

Programming Example 2:


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();
        }
    }
}


Output

Product Name: Monitor | Product Price : 3000
Product Name: SSD Disk | Product Price : 3500
Product Name: RAM | Product Price : 2450

_

Summary

In this tutorial, you learned how to use Where clause in Linq tutorial, in order to filter row. In the next chapter, you will learn Select keyword in Linq Tutorial.

 

Share your thought