Chapter 8. LINQ
LINQ, short for Language Integrated Query, provides a powerful set of
mechanisms for working with collections of information, along with a
convenient syntax. You can use LINQ with the arrays and lists we saw in the
previous chapter—anything that implements IEnumerable<T>
can be used with LINQ, and
there are LINQ providers for databases and XML documents. And even if you
have to deal with data that doesn’t fit into any of these categories, LINQ
is extensible, so in principle, a provider could be written for more or less
any information source that can be accessed from .NET. This chapter will
focus mainly on LINQ to Objects—the provider for running queries against
objects and collections—but the techniques shown here are applicable to
other LINQ sources.
Collections of data are ubiquitous, so LINQ can have a profound effect on how you program. Both of your authors have found that LINQ has changed how we write C# in ways we did not anticipate. Pre-LINQ versions of C# now feel like a different and significantly less powerful language. It may take a little while to get your head around how to use LINQ, but it’s absolutely worth the effort.
LINQ is not a single language feature—it’s the culmination of several elements that were added to version 3.0 of the C# language and version 3.5 of the .NET Framework. (Despite the different version numbers, these did in fact ship at the same time—they were both part of the Visual Studio 2008 release.) So as well as exploring the most ...
Get Programming C# 4.0, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.