Chapter 10. Presenting LINQ

One of the main additions to the .NET Framework for the v3.5 release is Language Integrated Query (LINQ), a new application programming interface (API) that is essentially a collection of namespaces and classes with a common purpose: to query data from any and all sources.

You might well wonder why Microsoft has chosen to provide yet another way to do what data source objects and ADO.NET do perfectly well already. Isn’t LINQ a bit pointless? Well, no. You saw in Chapter 7 how .NET DataSource objects provide a level playing field for working with data in different sources—whether it is a business object, an XML file, or a database. You also saw in Chapter 9 how ADO.NET offers more precise control of access to databases. But let’s take a step back and think about how you work with data from day to day:

  • Very rarely are you in the position where all the data you need is in the same source. Some may be in a database, some in business objects, some on the end of a web service, and so on.

  • The ease with which you can access data is entirely dependent on where it is stored. Accessing an in-memory object is much simpler than accessing data in an XML file or database.

  • The raw data itself is not often the end product. Once collated, it needs to be sorted, altered, grouped, ordered, looped over, merged into one data pool, and so on. Take the following code snippet, for example:

List<Book> books = GetBooks(); //sort books.SortByPrice(delegate(Book first, Book second)) { ...

Get Programming ASP.NET 3.5, 4th 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.