C# and LINQ – Filter elements from object collection using where clause

Published on November 18, 2016 by Senthil Kumar

Filed under C#

Last modified November 18, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 3 (4 votes)

This article have been viewed 14302 times

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the where clause using LINQ in C#. This sample gets all the employees who salary is greater than 20000 GBP.

How to Filter Employees List whose salary is greater than 20000 GBP using LINQ in C# ?

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AbundantcodeCsharpSample
    class Program
        static void Main(string[] args)
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { Name = "Michael", Age = 55, Salary = 80000 });
            employees.Add(new Employee { Name = "Steve", Age = 23, Salary = 19000 });
            employees.Add(new Employee { Name = "John", Age = 55, Salary = 23000 });
            var output = from m in employees
                         where m.Salary > 20000
                         select m;
            Debug.WriteLine("Employees whose salary is greater than 20000 pounds");
            foreach (var item in output)
    class Employee
        public string Name { get; set; }
        public int Age { get; set; }
        public decimal Salary { get; set; }


Employees whose salary is greater than 20000 pounds



Leave a Comment


You might be Interested in these Posts

How to get the AM or PM value from a DateTime object in C# ?

There are times when you might want to get only the string AM or PM from the DateTime object that yo...

C# program to print the multiplication table of a given number.

Problem Write a program in Microsoft Visual C# that takes a number as input and prints its multipli...

C# Program to swap two numbers without using temporary variable

Problem Write a program in Visual CSharp to swap two numbers using temporary variable and display t...