Filtering with LINQ

Language Integrated Queries (LINQ) are powerful SQL-like statements that are built-in to the .NET Framework. They allow you to query a wide range of collections, including:

  • XML
  • Enumerable Classes
  • Arrays
  • SQL Databases

I use them a lot to filter data in memory.

“How do I utilize LINQ queries?” you ask.

The syntax is very similar to SQL:

var filteredData = from p in myCollection select p;

The above query selects everything from an arbitrary collection named “myCollection”. This is not very useful if we need to get a subset of this data.

var filteredData = from p in myCollection where p.FirstName == "Joe" select p;

This query gets everything from “myCollection” where the FirstName property (or field) is equal to “Joe”. This gets everyone with a first name of “Joe”.

Examples

Let’s pretend we have a collection of persons we want to filter. A sample POCO would look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FilteringWithLINQ
{
    class Person
    {
        public int Age { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }

    }
}

Notice that we have properties for Age, FirstName, LastName, and BirthDate.

To setup a sample list of persons, we might do something like:

private static void SetupPeople()
        {
            var people = new List<Person>();

            people.Add(new Person() { FirstName = "Joe", LastName = "Williams", Age = 21, BirthDate = DateTime.Parse("1/1/1993") });
            people.Add(new Person() { FirstName = "Steven", LastName = "Williams", Age = 31, BirthDate = DateTime.Parse("1/12/1983") });
            people.Add(new Person() { FirstName = "John", LastName = "Doe", Age = 41, BirthDate = DateTime.Parse("1/1/1973") });
            people.Add(new Person() { FirstName = "Jane", LastName = "Doe", Age = 51, BirthDate = DateTime.Parse("1/1/1963") });
            people.Add(new Person() { FirstName = "Aaron", LastName = "Neville", Age = 100, BirthDate = DateTime.Parse("1/1/1914") });

        }

This generates a list of persons (people).

What if we want to get only people between the ages of 31 and 50?

var filterByAgeRange = from p in people where p.Age >= 31 && p.Age <= 50 select p;

That’s it! It is as simple as adding the filter in the where clause of the LINQ statement using familiar C# syntax.

What are some other ways to get people by an age range using LINQ?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s