Chris Pietschmann

husband, father, hacker, entrepreneur, futurist, innovator, autodidact


Introduction to LINQ

What is LINQ?

LINQ (Language INtegrated Query) is more than just a new method of embedding SQL queries within code. It allows to to perform Strongly Typed queries on any kind of data, just as long as the collection of data implements the IEnumerable interface.

The most important part is "Language Integrated". This allows you to more easily write queries in C# 3.0 and VB 9.0 such as:


IEnumerable<Person> filterExample1 = from Person in people
where Person.FirstName.ToUpperInvariant().StartsWith("J")
                                                                select Person;


Instead of:


IEnumerable<Person> filterExample2 = people
                                                            .Where(p => p.FirstName.ToUpperInvariant().StartsWith(
                                                            .Select(p => p);

As you can see the second option takes a little longer to write and isn't quite as easy to read as the first option or a standard SQL query. It essentially allows us to query our data in a SQL-like fashion that we're already familiar with.

The second important part of LINQ (and probably just as important) is since all you query code is written in .NET, you get the benefit of things being Strongly Types, and the compiler type checking the code at compile time. You'll no longer get strange type exceptions at run-time with LINQ that you would traditionally get if you used SQL string within your code.

There's more than one type of LINQ?

Yes, but don't get scared away just yet. The different types of LINQ are for querying different types of data. The differnet types of LINQ are as follows:

  • LINQ to Objects - This type is geared towards working with collections of Objects, hence the name.
  • LINQ to SQL - This type is geared towards working with data from a database.
  • LINQ to XML - This type is geared toward working with data in XML documents.

In the rest of this article we'll be using LINQ to Objects.

Basic Query Syntax

First lets look at the following example; which filters out all the Person objects with a FirstName that starts with the letter "J":


IEnumerable<Person> filterExample1 = from Person in people
where Person.FirstName.ToUpperInvariant().StartsWith("J")
                                                                    select Person;


First we declare the collection of data we're going to query from using the "from" operator, "from Person in people". "people" is our collection that inherits from IEnumerable<T> and "Person" is type of object that collection contains.

Second we define any query criteria using the "where" operator. You can define as many where clauses as necessary.

And lastly, we declare the data we're going to select using the "select" operator.

One thing to note when using LINQ: you must define the data to select Last, as compared to First with the SQL language.

Other Simple LINQ Examples

"orderby" Operator: The "orderby" operator allows you to sort your query results.


IEnumerable<Person> orderByAge = from Person in people
                                                                    orderby Person.Age
                                                                    select Person;


"let" Operator: The "let" operator allows you to declare a new variable within the scope of the query. This new variable can only be used by query clauses that are declared after the use of the "let" operator.


IEnumerable<Person> letExample1 = from Person in people
let firstname = Person.FirstName.ToUpperInvariant()
                                                                where firstname.StartsWith("J")
                                                                select Person;


Select Distinct Items: You can use the Distinct() method to grab just the distinct items in the collection.


IEnumerable<String> distinctExample2 = (from Person in people
select Person.FirstName).Distinct();



As you can see, LINQ is rather simple to use the basics of. However, it is rather involved and will definately take some time to master.

For further reading:

blog comments powered by Disqus