Where Linq Example

The Where Linq operator, probably the most commonly used, allows you to filter or find things where a certain boolean condition is met. Let's check out a few examples.

A basic example

To get started with, let's say we want to filter a list of numbers where all numbers are greater than 70.

Method syntax

Here, the lambda expression num > 70 can be used as a condition.

Live example: https://dotnetfiddle.net/XDWEKR

int[] numbers = { 40, 10, 24, 60, 100, 55, 80, 75, 35 };

var numbersAboveSeventy = numbers.Where(num => num > 70);

// output: 100, 80, 75

Query syntax

Using the query syntax, the same lambda expression can be also be used num > 70.

Live example: https://dotnetfiddle.net/Y2Qi2j

int[] numbers = { 40, 10, 24, 60, 100, 55, 80, 75, 35 };

var numbersAboveSeventy = from num in numbers 
                         where num > 70
                         select num;

// output: 100, 80, 75

Using contains - filtering from another list

If you had two lists, say set1 and set2 and you wanted to find out all those numbers in set1 that also exist in set2 only. How does one go about doing this? Well, we can use the same Where operator, with the addition of the Contains method.

Method syntax

So we have two arrays, set1, and set2. We want to filter set1 so that only numbers that appear in set1 also are contained in set2. The output as you can see is just 100 as this is the only number that appears in set2.

Live example: https://dotnetfiddle.net/VZm2W6

int[] set1 = { 40, 10, 24, 60, 100, 55, 80, 75, 35 };
int[] set2 = { 120, 110, 150, 160, 100, 155, 180, 175, 135 };

var results = set1.Where(i => set2.Contains(i));

//output: 100

Query syntax

The query syntax is very similar and uses the Contains method.

Live example: https://dotnetfiddle.net/f8UkzT

int[] set1 = { 40, 10, 24, 60, 100, 55, 80, 75, 35 };
int[] set2 = { 120, 110, 150, 160, 100, 155, 180, 175, 135 };


var results = from s1 in set1
              where set2.Contains(s1)
              select s1;
                
//output: 100

Where example with a list of objects

Let's take a look at a more real-world example with objects. Another common activity is to filter a list of objects based on a certain condition.

Check out the example below where we filter an array of customers that have a Visa card and the ExpiryYear is less than or equal to 2014

Method syntax

In the example below, we have an anonymous object which describes a customer payment profile. We want to find all users where the card is a Visa and the expiry year is less than and equal to 2015. Similar to the first basic example above, we can use a lambda expression, such as customer.Card == "Visa" && customer.ExpiryYear <= 2015.

Live example: https://dotnetfiddle.net/Y8ZPLN

var customers = new[] {
    new{
            Name = "Vernon",
            DateOfBirth = "1994-Jun-25",
            Active = true,
            Card = "MasterCard",
            CardNumer = "*** 1142",
            ExpiryYear = 2022,
            ExpiryMonth = 7
        },
    new {
            Name = "Carrie",
            DateOfBirth = "1986-Feb-01",
            Active = false,
            Card = "Visa",
            CardNumer = "*** 2156",
            ExpiryYear = 2015,
            ExpiryMonth = 7
        },
    new {
            Name = "Joanna",
            DateOfBirth = "1972/10/13",
            Active = true,
            Card = "Visa",
            CardNumer = "*** 7683",
            ExpiryYear = 2014,
            ExpiryMonth = 3
        },
    new {
            Name = "Louis",
            DateOfBirth = "1975/05/10",
            Active = true,
            Card = "Visa",
            CardNumer = "*** 7683",
            ExpiryYear = 2016,
            ExpiryMonth = 2
        }
};

var results = customers.Where(customer => customer.Card == "Visa" 
                    && customer.ExpiryYear <= 2015);

//output: Carrie, Joanna

Query syntax

Again, similar to the method syntax, the same boolean lambda expression can be used.

Live example: https://dotnetfiddle.net/gGF2Yi

var customers = new[] {
    new{
            Name = "Vernon",
            DateOfBirth = "1994-Jun-25",
            Active = true,
            Card = "MasterCard",
            CardNumer = "*** 1142",
            ExpiryYear = 2022,
            ExpiryMonth = 7
        },
    new {
            Name = "Carrie",
            DateOfBirth = "1986-Feb-01",
            Active = false,
            Card = "Visa",
            CardNumer = "*** 2156",
            ExpiryYear = 2015,
            ExpiryMonth = 7
        },
    new {
            Name = "Joanna",
            DateOfBirth = "1972/10/13",
            Active = true,
            Card = "Visa",
            CardNumer = "*** 7683",
            ExpiryYear = 2014,
            ExpiryMonth = 3
        },
    new {
            Name = "Louis",
            DateOfBirth = "1975/05/10",
            Active = true,
            Card = "Visa",
            CardNumer = "*** 7683",
            ExpiryYear = 2016,
            ExpiryMonth = 2
        }
};

var results = from c in customers
              where c.Card == "Visa" && c.ExpiryYear <= 2015
              select c;

//output: Carrie, Joanna