Chapter 11
Language Integrated Query
LINQ (Language Integrated Query) is the most important new feature of C# 3.0 and .NET 3.5. LINQ integrates query syntax inside the C# programming language and makes it possible to access different data sources with the same syntax. LINQ makes this possible by offering an abstraction layer.
This chapter gives you the core foundation of LINQ and the language extensions for C# 3.0 that make the new features possible. The topics of this chapter are:
- Traditional queries across objects using List<T>
- Extension methods
- Lambda expressions
- LINQ query
- Standard query operators
- Expression trees
- LINQ providers
This chapter gives you the core foundation of LINQ. For using LINQ across the database you should read Chapter 27, “LINQ to SQL.” To query XML data read Chapter 29, “LINQ to XML,” after reading this chapter.
LINQ Overview
Before getting into the features of LINQ, this section uses an example to show how queries across objects were done before LINQ was available. As you read on, the query will evolve to show how the LINQ query is reached. By going through the steps you will know what’s behind the LINQ query.
The example in this chapter is based on Formula-1 world champions. Queries are done across a list of Racer objects. The first query gets all Formula-1 champions from Brazil in the order of races won.
Query using List<T>
The first variant of a filter and sort is to search data in a list of type List<T>. Before the search can start, the object type ...