Ebooks

C# filter list tutorial

C# filter list tutorial shows how to filter a list in C#. C# tutorial is a comprehensive tutorial on C# language.

This tutorial shows several ways to filter a list in C# language. We use iteration, LINQ, and built-in FindAll() method.

C# filter list with iteration

In the first example, we use a foreach loop to filter a list.

Program.cs
using System;
using System.Collections.Generic;

namespace FilterListIteration
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "sky", "rock", "forest", "new",
                "falcon", "jewelry" };

            var filtered = new List<string>();

            foreach (var word in words)
            {
                if (word.Length == 3)
                {
                    filtered.Add(word);
                }
            }

            Console.WriteLine(string.Join(',', filtered));
        }
    }
}

The example filters out all words that have three characters.

var words = new List<string> { "sky", "rock", "forest", "new",
    "falcon", "jewelry" };

We have a list of words. The goal is to find out all words with three letters.

var filtered = new List<string>();

A new filtered list is created. All the words that match the condition will be added to the list.

foreach (var word in words)
{
    if (word.Length == 3)
    {
        filtered.Add(word);
    }
}

We go over the list of words in a foreach loop. All words that match the if condition are added to the filtered list.

Console.WriteLine(string.Join(',', filtered));

We show the contents of the filtered list to the console.

$ dotnet run
sky,new

This is the output.

C# filter list with FindAll

In the following example, we filter a list with the built-in FindAll() method.

Program.cs
using System;
using System.Collections.Generic;

namespace FilterListFindAll
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = new List<int> {-1, -3, 0, 1, 3, 2, 9, -4};

            List<int> filtered = vals.FindAll(e => e > 0);

            Console.WriteLine(string.Join(',', filtered));
        }
    }
}

The example finds out all integer values that are greater than zero.

List<int> filtered = vals.FindAll(e => e > 0);

The FindAll() method retrieves all the elements that match the conditions defined by the specified predicate.

$ dotnet run
1,3,2,9

This is the output.

C# filter list with LINQ query expression

The following example uses a LINQ query expression to filter a list.

Program.cs
using System;
using System.Linq;
using System.Collections.Generic;

namespace FilterListLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "sky", "rock", "forest", "new",
                "falcon", "jewelry" };

            IEnumerable<string> query = from word in words
                                        where word.Length == 3
                                        select word;

            foreach (var word in query)
            {
                Console.WriteLine(word);
            }
        }
    }
}

The example selects all words that have three characters.

C# filter list with LINQ Where

The next example filters a list with LINQ's Where() method.

Program.cs
using System;
using System.Linq;
using System.Collections.Generic;

namespace FilterListLinqWhere
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = new List<int> {-1, -3, 0, 1, 3, 2, 9, -4};

            List<int> filtered = vals.Where(x => x > 0).ToList();

            Console.WriteLine(string.Join(',', filtered));
        }
    }
}

The example filters out all positive values.

List<int> filtered = vals.Where(x => x > 0).ToList();

The Where() method filters a sequence of values based on a predicate.

C# filter a list of objects

In the following example we filter a list of car objects with a LINQ query expression.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace FilterListCars
{
    public class Car
    {
        public string Name { get; set; }
        public int Price { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Car> cars = new List<Car>
            {
                new Car{ Name = "Audi", Price=52642 },
                new Car{ Name = "Mercedes", Price=57127 },
                new Car{ Name = "Skoda", Price=9000 },
                new Car{ Name = "Volvo", Price=29000 },
                new Car{ Name = "Bentley", Price=350000 },
                new Car{ Name = "Citroen", Price=21000 },
                new Car{ Name = "Hummer", Price=41400 },
                new Car{ Name = "Volkswagen", Price=21600 }
            };

            foreach (var car in from car in cars
                                where car.Price > 9000 && car.Price < 50000
                                select new { car.Name, car.Price })
            {
                Console.WriteLine($"{car.Name} {car.Price}");
            }

        }
    }
}

The example selects all cars whose price is between 9000 and 50000.

$ dotnet run
Volvo 29000
Citroen 21000
Hummer 41400
Volkswagen 21600

These cars satisfy the specified condition.

In this tutorial, we have showed how to filter a list in C#.

Read C# tutorial or list all C# tutorials.