Chapter 10. LINQ

Language Integrated Query (LINQ) is a powerful group of tools for working with sets of information in C#. It is useful in any application that needs to work with multiple pieces of data (i.e., almost any application). Although one of its primary goals was to provide straightforward access to relational databases, LINQ is applicable to many kinds of information. For example, it can also be applied to in-memory object models, HTTP-based information services, and XML documents.

LINQ is not a single feature. It relies on several language elements that work together. The most conspicuous LINQ-related language feature is the query expression, a form of expression that loosely resembles a database query but can be used to perform queries against any supported source, including plain old objects. As you’ll see, query expressions rely heavily on some other language features such as lambdas, extension methods, and expression object models.

Language support is only half the story. LINQ needs class libraries to implement a standard set of querying primitives called LINQ operators. Each different kind of data requires its own implementation, and a set of operators for any particular type of information is referred to as a LINQ provider. (These can also be used from Visual Basic and F#, by the way, because those languages support LINQ too.) The .NET Framework class library has several built-in providers, including one for working directly with objects (called LINQ to Objects) and ...

Get Programming C# 5.0 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.