The C# Station LINQ Tutorial

by Joe Mayo, 11/8/2011, 2/16/2013

Lesson 01: Introduction to LINQ

This lesson provides essential concepts to learning Language Integrated Query (LINQ). It helps you get started and forms the basis for later lessons. Here are the objectives:
  • Understand What LINQ Is.
  • Learn What Problems LINQ Solves.
  • Quickly See What LINQ Syntax Looks Like.
  • Know Where LINQ can be Used.

What is LINQ?

LINQ is an acronym for Language Integrated Query, which is descriptive for where it's used and what it does. The Language Integrated part means that LINQ is part of programming language syntax. In particular, both C# and VB are languages that ship with .NET and have LINQ capabilities. Another programming language that supports LINQ is Delphi Prism. The other part of the definition, Query, explains what LINQ does; LINQ is used for querying data. Notice that I used the generic term "data" and didn't indicate what type of data. That's because LINQ can be used to query many different types of data, including relational, XML, and even objects. Another way to describe LINQ is that it is programming language syntax that is used to query data.
Note: In addition to a new language syntax, LINQ can be used via a fluent API that chains methods together. The bulk of this tutorial will concentrate on the C# language syntax that supports LINQ.

We Already have ADO.NET, so Why Another Data Access Technology?

Most applications work with data in one form or another, meaning that data is very important to the work we do as software engineers. It's so important that the tools we use are constantly evolving, with the next generation building and improving upon the previous. This doesn't change with LINQ, which is the next giant leap in data development technology beyond ADO.NET.
ADO.NET is an object-oriented library, yet we must still reason about data from a relational perspective. In simple scenarios, we can bind ADO.NET objects directly to user interfaces (UI), but many other situations require the translation of the ADO.NET data into business objects with relationships, rules, and semantics that don't translate automatically from a relational data store. For example, a relational data store will model Orders and Customers with a foreign key from an Order table to a Customer table, but the object representation of this data is a Customer object with a collection of Order objects. Similar situations occur for other storage types such as hierarchical, multi-value, and flat data sources. This gap between the representation of data from storage site to the objects you use in your applications is called an Impedence Mismatch. While ADO.NET is an object library for working with relational data, LINQ is a SQL-like syntax that produces usable objects. LINQ helps reduce this Impedence Mismatch.
Note: The operative term in the previous paragraph is "reduce". LINQ does not eliminate Impedence Mismatch, because you must still reason about your data store format. However, LINQ does remove a lot of the plumbing work you have to do to re-shape your data as an object.

A Quick Peek at LINQ Syntax

Now that you know what LINQ is and why it was created, you'll see an example of LINQ syntax. The example you'll see uses a C# array as the data source. The array contains musical artists names. The purpose of the LINQ query is to read specified items from the data source and return them in a collection. Later lessons will provide more information about the parts of this query, but what you see here is only to give you a quick look at LINQ syntax so you can get a feel for what LINQ is like. Listing 1-1 shows the LINQ query just described:
Listing 1-1: Sample LINQ Query
            string[] musicalArtists = { "Adele", "Maroon 5", "Avril Lavigne" };

            IEnumerable<string> aArtists =
                from artist in musicalArtists
                where artist.StartsWith("A")
                select artist;

            foreach (var artist in aArtists)
            {
                Console.WriteLine(artist);
            }
In Listing 1-1, musicalArtists is a string[], but could be any IEnumerable collection. Here, we're using LINQ to Objects and the Objects part is any IEnumerable collection. Since, C# arrays are IEnumerable, they can be queried with LINQ to Objects. I won't spell everything out as LINQ to Objects going forward, but if there's a difference in the data source, I'll spell it out.
The aArtists variable is an IEnumerable<string>, which is the result of the query; a collection of objects. The query has three clauses: fromwhere, and select. The from clause has a range variableartist, which holds an individual reference for each object of the data source, aArtists. The where clause filters the result, returning only objects (string in this case) who's first letter is 'A'. The select clause returns the part of the object you want to return, referred to as aprojection. In this example, the projection is the entire object, which is a string. Later lessons dig into each of these clauses and more, but this was just a quick overview to whet your appetite and let you see how easy and natural it is to write LINQ code.

More about Data Sources

LINQ is intended to make it easy to query data sources. One of the more popular uses of LINQ is to query relational databases. However, as you see here, LINQ can query objects. That's not all, the .NET Framework includes libraries that allow anyone to create a LINQ provider that can query any data source. Out of the box, Microsoft ships LINQ to Objects, LINQ to XML, LINQ to SQL (SQL Server), and LINQ to Entities (Entity Framework). There are also 3rd party LINQ providers that make it easy to query specialized data sources. For example, I wrote a LINQ provider lets you query the Twitter API, LINQ to Twitter.
Much of this tutorial shows you how to use LINQ to Objects, but the same concepts are applicable to other LINQ providers. There are nuances between each provider, but one thing is consistent: the skills you learn for LINQ are generally reusable accross all LINQ providers.

Summary

Now you know what LINQ is, a new feature of programming languages that allow you to query data. You learned how LINQ differs from and improves upon ADO.NET. Additionally, remember that LINQ helps reduce Impedence Mismatch between data storage and the objects you code with every day. Listing 1-1 gave you a quick example of what a LINQ query looks like, with a brief explanation of the new syntax clauses. Finally, you learned about how LINQ can be used for many different types of data sources, in addition to the frequent use of LINQ to Objects in this tutorial.
This is just the beginning - the first of several lessons in the LINQ Tutorial. In the next lesson, Lesson 02: Forming Projections, you'll learn more about the select clause.

Comments