Linq to Amazon implementation fore steps
var query =
from book in new Amazon.BookSearch()
where
book.Title.Contains("ajax") &&
(book.Publisher == "Manning") &&
(book.Price <= 25) &&
(book.Condition == BookCondition.New)
select book;
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:
interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
interface IEnumerable
{
IEnumerator GetEnumerator();
}
interface IQueryable : IEnumerable
{
Type ElementType { get; }
Expression Expression { get; }
IQueryable CreateQuery(Expression expression);
object Execute(Expression expression);
}
and finally the IQueryable<T> interface itself of course:
interface IQueryable<T> : IEnumerable<T>, IQueryable, IEnumerable
{
IQueryable<S> CreateQuery<S>(Expression expression);
S Execute<S>(Expression expression);
}
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:
var query =
from book in new Amazon.BookSearch()
where
book.Title.Contains("ajax") &&
(book.Publisher == "Manning") &&
(book.Price <= 25) &&
(book.Condition == BookCondition.New)
select book;
the compiler translates this into:
IQueryable<Book> query = Queryable.Where<Book>(new BookSearch(), <expression tree>);
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
And why is it called a "tree"? Because it's a hierarchy of expressions. Here is the complete expression tree in our case:
Expression.Lambda<Func<Book, Boolean>>(
Expression.AndAlso(
Expression.AndAlso(
Expression.AndAlso(
Expression.CallVirtual(
typeof(String).GetMethod("Contains"),
Expression.Field(book, typeof(Book).GetField("Title")),
new Expression[] { Expression.Constant("ajax") }),
Expression.Call(
typeof(String).GetMethod("op_Equality"),
null,
new Expression[] {
Expression.Field(book, typeof(Book).GetField("Publisher")),
Expression.Constant("Manning") })),
Expression.Call(
typeof(Decimal).GetMethod("op_LessThanOrEqual"),
null,
new Expression[] {
Expression.Field(book, typeof(Book).GetField("Price")),
Expression.Constant(new Decimal(25), typeof(Decimal)) })),
Expression.EQ(
Expression.Convert(
Expression.Field(book, typeof(Book).GetField("Condition")),
typeof(BookCondition)),
Expression.Constant(BookCondition.New))),
new ParameterExpression[] { book }));
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!
Cross-posted from http://linqinaction.net