Chapter 23. Introduction to LINQ

WHAT YOU WILL LEARN IN THIS CHAPTER

  • Coding a LINQ query and the parts of a LINQ query statement

  • Using LINQ method syntax versus LINQ query syntax

  • Ordering query results, including ordering on multiple levels

  • When and how to use LINQ aggregate operators

  • Using projection to create new objects in queries

  • Using the Distinct(), Any(), All(), First(), FirstOrDefault(), Take(), and Skip() operators

  • Group queries

  • Set operators and joins

This chapter introduces Language Integrated Query (LINQ). LINQ is an extension to the C# language introduced in C# 3.0, which preceded the C# 4 language supported in Visual Studio 2010. LINQ solves the problem of dealing with very large collections of data, for which you typically need to select a subset of the collection for the task your program is performing.

In the past, this sort of work required writing a lot of looping code, and additional processing such as sorting or grouping the found objects required even more code. LINQ frees you from having to write this extra looping code to filter and sort. It enables you to focus on the objects that matter to your program.

In addition to providing an elegant query language that enables you to specify exactly what objects you are searching for, LINQ offers many extension methods that make it easy to sort, group, and calculate statistics on your query results.

With LINQ, you can query many different data sources in C#, including objects, SQL databases, XML documents, entity data models, ...

Get Beginning Visual C# 2010 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.