ASP.NET Hosting

Linq to Amazon implementation fore steps

On Monday, I have announced an implementation of Linq for Amazon Web Services, that allows to query for books using the following syntax:

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?
  1. To defer the execution of the query
  2. 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:

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

2 Comments

  • How can you give hints to LINQ. That is to say in your sample query, you probably wouldn't want it to look at price until the previous conditions are met. Is it strictly by the order you introduce items in the where clause?

    Does LinQ optimize itself? That is to say would this query (different order) perform at the same level as your Query?

    (book.Price <= 25) &&
    (book.Condition == BookCondition.New) &&
    book.Title.Contains("ajax") &&
    (book.Publisher == "Manning")

  • In my case, I collect all the criteria regardless of the order, so yes, your query is equivalent to mine. But in fact, I would say that it's up to you. You can do whatever you want in your implementation. As you see in the expression tree, you get the expressions in the order they were formulated, so you can use that as critical information for the processing if you wish.

Comments have been disabled for this content.