How to Use Where in LINQ - C#

This is a quick guide on how to use the Where LINQ operator, using both method and query syntax.

The Where command allows us to filter a collection, based on a specified criteria.

Let’s take a look at an example with a simple object collection:

class UsingLinq 
{
    public static void Main (string[] args) 
    {
        var people = new List<Person>() 
        { 
            new Person() { PersonId = 1, Name = "Belinda", Age = 70 },
            new Person() { PersonId = 2, Name = "Betty",  Age = 81 },
            new Person() { PersonId = 3, Name = "Will",  Age = 18 },
            new Person() { PersonId = 4, Name = "Andy", Age = 70 },
            new Person() { PersonId = 5, Name = "Rob", Age = 15 }
        };
	}
}

class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

    public Person() {}

    public void Introduce() 
    {
        Console.WriteLine($"My name is {Name}, and I am {Age} years old.");
    }
}

Using Method Syntax

Using the method syntax, let’s use Where to filter the people list to only those that are older than 20.

public static void Main (string[] args) 
{
	var people = new List<Person>() 
	{ 
		new Person() { PersonId = 1, Name = "Belinda", Age = 70 },
		new Person() { PersonId = 2, Name = "Betty",  Age = 81 },
		new Person() { PersonId = 3, Name = "Will",  Age = 18 },
		new Person() { PersonId = 4, Name = "Andy", Age = 70 },
		new Person() { PersonId = 5, Name = "Rob", Age = 15 }
	};

	var result = people
		.Where(p => p.Age > 20);
 
	foreach(Person p in result)
	{
		p.Introduce();
	}
}

This will give us the following output:

My name is Belinda, and I am 70 years old.
My name is Betty, and I am 81 years old.
My name is Andy, and I am 70 years old.

Using Query Syntax

We can also use the query syntax, to achieve the same outcome:

public static void Main (string[] args) 
{
	var people = new List<Person>() 
	{ 
		new Person() { PersonId = 1, Name = "Belinda", Age = 70 },
		new Person() { PersonId = 2, Name = "Betty",  Age = 81 },
		new Person() { PersonId = 3, Name = "Will",  Age = 18 },
		new Person() { PersonId = 4, Name = "Andy", Age = 70 },
		new Person() { PersonId = 5, Name = "Rob", Age = 15 }
	};

	var result = from p in people 
				 where p.Age > 20
				 select p;
 
	foreach(Person p in result)
	{
		p.Introduce();
	}
}

This will give us the same output:

My name is Belinda, and I am 70 years old.
My name is Betty, and I am 81 years old.
My name is Andy, and I am 70 years old.

Combining Multiple Conditions

You can chain multiple conditions inside a single Where call using && and ||:

var result = people.Where(p => p.Age > 20 && p.Name.StartsWith("B"));

foreach(Person p in result)
{
	p.Introduce();
}
My name is Belinda, and I am 70 years old.
My name is Betty, and I am 81 years old.

Where vs Select

Where filters rows from a collection; Select transforms each element into a new shape. You’ll often chain them together — filter first, then project. See How to Use Select in LINQ for a full guide on Select.

var names = people
	.Where(p => p.Age > 20)
	.Select(p => p.Name);

foreach(string name in names)
{
	Console.WriteLine(name);
}
Belinda
Betty
Andy