Lesson 01 Introduction To LINQ
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.
IEnumerable<string> aArtists =
from artist in musicalArtists
where artist.StartsWith("A")
select 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: from, where, and select. The from clause has a range variable,
artist, 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 a projection. 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.
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.