Before getting to the details of the implementation code, I'd like to describe what we need to do in order to be able to use such code.First, as you can see we work with book objects. So, let's defined a Book class:

public class Book{ public IList<String> Authors; public BookCondition Condition; public String Publisher; public Decimal Price; public String Title; public UInt32 Year;}

Here I use public fields for the sake of simplicity, but properties and private fields would be better.
You can see that this class defines the members we use in our query: Title, Publisher, Price and Condition, as well as others we'll use later for display. Condition is of type BookCondition, which is just an enumeration defined like this:

public enum BookCondition {All, New, Used, Refurbished, Collectible}

The next and main thing we have to do is define this BookSearch class we use to perform the query. This class implements System.Query.IQueryable<T> to receive and process query expressions. IQueryable<T> is defined like this:

interface IQueryable<T> : IEnumerable<T>, IQueryable

which means that we have to implement the members of the following interfaces:

It may look like a lot of work... I will try to describe it simply so that you can create your own implementation of IQueryable without too much difficulty once you get to know how the mechanics work.

In order to be able to implement IQueryable, you need to understand what happens behind the scenes. The from..where..select query expression you write in your code is just syntactic sugar that the compiler converts quietly into something else! In fact, when you write:

Queryable .Where is a static method that takes as arguments an IQueryable followed by an expression tree.I can hear you crying out loud: "What the hell is an expression tree?!".Well, an expression tree is just a way to describe what you wrote after where as data instead of code. And what's the point?

To defer the execution of the query

To be able to analyze the query to do whatever we want in response

In our case, we will go to the web and ask Amazon to return some XML data about books, but we could also translate the query into SQL and execute it against a database (this is what Linq to SQL does!), or do anything else you'd consider useful.

And why is it called a "tree"? Because it's a hierarchy of expressions. Here is the complete expression tree in our case:

If you look at this tree, you should be able to locate the criteria we have specified in our query. What we will do in the next step is see how all this combines and how we will extract the information from the expression tree to be able to construct a web query to Amazon.We'll keep that for another post... Stay tuned!