concept LinqBooks in category .net

appears as: The LinqBooks, LinqBooks, LinqBooks
LINQ in Action

This is an excerpt from Manning's book LINQ in Action.

In this chapter, we will look at a sample application we’ve created using LINQ. This application is the LinqBooks running example that we introduced in chapter 4 and that we used over this book’s chapters as the base for the code samples. You’ll be able to find the complete source code in the downloadable package that accompanies this book. By looking at the LinqBooks sample, you’ll be able to identify where and how each LINQ flavor is used. Our goal is to help you decide whether you need to use LINQ in your application layers, as well as see what impact it can have on your applications’ architecture.

The first custom query operator that is used in LinqBooks is TotalPrice. We created this query operator in chapter 12. It iterates over an enumeration of books and returns the sum of the books’ prices. This operator demonstrates how you can create custom query operators to simplify your code. For example, once you’ve created the TotalPrice operator, getting the total price of all books in your LinqBooks catalog can be achieved with the following simple code:

var dataContext = new LinqBooksDataContext();
lblTotalPrice.Text = dataContext.Books.TotalPrice().ToString("F2");

Another custom query operator used in LinqBooks comes from chapter 5: MaxElement. The goal of this operator is to retrieve an object from a collection.

Let’s now focus on some code samples to give you an idea of how LINQ to SQL can be used in an application like LinqBooks.