Testing if Any Element in a C# List Meets a Condition

A common problem we developers have to deal with is determining if one or more items in a list meets a condition. We don't care if the whole list meets the condition, so long as at least one item meets the condition. For example, let's say you have a list of animals and their vaccination history; you need to find if any animals in the list are unvaccinated. We can use the LINQ extension Any to find any item in the list meets the criteria. LINQ Any returns true if any item matches the criteria, or false if all items do not match the criteria.

Microsoft Explanation

Determines whether any element of a sequence satisfies a condition.

Using the 2.0 method, we might use something like this:

C# 2.0 Example

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Vaccinated { get; set; }
}

public static void AnyEx()
{
    // Create an array of Pets.
    List<Pet> PetList =
    {
        new Pet { Name="Barley", Age=8, Vaccinated=true },
        new Pet { Name="Boots", Age=4, Vaccinated=false },
        new Pet { Name="Whiskers", Age=1, Vaccinated=false }
    };

    // set our flag
    bool unvaccinated = false;

    // Determine whether any pets over age 1 are also unvaccinated.
    foreach (Pet pet in PetList)
    {
        // check for age over 1 and unvaccinated
        if ((pet.Age > 1) && (! pet.Vaccinated))
        {
            // found a match, set flag and exit loop
            unvaccinated = true;
            break;
        }
    }

    Console.WriteLine(
        "There {0} unvaccinated animals over age one.",
        unvaccinated ? "are" : "are not any");
}

// This code produces the following output:
//
//  There are unvaccinated animals over age one.
 

 

With LINQ,  we can write this, instead:

C# LINQ Any Example

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Vaccinated { get; set; }
}

public static void AnyEx2()
{
    // Create an array of Pets.
    List<Pet> PetList =
    {
        new Pet { Name="Barley", Age=8, Vaccinated=true },
        new Pet { Name="Boots", Age=4, Vaccinated=false },
        new Pet { Name="Whiskers", Age=1, Vaccinated=false }
    };

    // Determine whether any pets over age 1 are also unvaccinated.
    bool unvaccinated = PetList.Any(p => p.Age > 1 && p.Vaccinated == false);

    Console.WriteLine(
        "There {0} unvaccinated animals over age one.",
        unvaccinated ? "are" : "are not any");
}

// This code produces the following output:
//
//  There are unvaccinated animals over age one.

 

As you can see, using LINQ Any is a powerful way to quickly check for the existence of item or criteria in a list.

If have questions, comments, or suggestion, please feel to post them in the LINQ Exchange Forum

GD Star Rating
loading...
GD Star Rating
loading...
Testing if Any Element in a C# List Meets a Condition, 1.0 out of 5 based on 1 rating
  • Share/Bookmark

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply