Chapter 13. Introducing LINQ

One of the common programming tasks C# programmers perform every day is finding and retrieving objects in memory, a database, or an XML file. For example, you may be developing a cell phone customer support system that will allow a customer to see how much each member of the family has spent in phone calls. To do so, you’ll need to retrieve records from various sources (phone company records online, phone books kept locally, etc.), filtered by various criteria (by name or by month), and sorted in various ways (e.g., by date, by family member).

One way you might have implemented this in the past would be to search a database by address, returning all the records to the user, perhaps presenting them in a listbox. The user would pick the name she was interested in and the data of interest (e.g., the number of ringtones downloaded in the past three months), and you would go back to the database (or perhaps to a different database) and retrieve that information, using the chosen family member’s unique ID as a key, retrieving the required data.

Although C# provides support for in-memory searches such as finding a name in a collection, traditionally, you were required to turn to another technology (such as ADO.NET) to retrieve data from a database. Although ADO.NET made this fairly easy, a sharp distinction was drawn between retrieving data from in-memory collections and retrieving data from persistent storage.

In-memory searches lacked the powerful and flexible ...

Get Programming C# 3.0, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.