LINQ in Action - LINQ Book & News

Introducing Linq to Amazon

As an example that will be included in the Linq in Action book, I've created an example that shows how Linq can be extended to query anything.
This example introduces Linq to Amazon, which allows querying Amazon for books using Linq! It uses Linq's extensibility to allow for language-integrated queries against a book catalog. The Linq query gets converted to REST URLs supported by Amazon's web services. These services return XML. The results are converted from XML to .NET objects using Linq to XML.

For the moment, let's look at the client code:

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;


I think this code speaks for itself! This is Linq to Amazon code. It expresses a query against Amazon, but does not execute it... The query will be executed when we start enumerating the results.

The following piece of code converts from Linq to Amazon to Linq to Objects:

var sequence = query.ToSequence();

The query gets executed this time and an enumeration of the resulting books is created.
The next steps could be to use Linq to Objects to perform grouping operations on the results. e.g.:

var groups =
  from book in query.ToSequence()
  group book by book.Year into years
  orderby years.Key descending
  select new {
    Year = years.Key,
    Books =
      from book in years
      select new { book.Title, book.Authors }
  };


This allows to display the results like this:

Published in 2006
  Title=Ruby for Rails : Ruby Techniques for Rails Developers    Authors=...
  Title=Wxpython in Action    Authors=...

Published in 2005
  Title=Ajax in Action    Authors=...
  Title=Spring in Action (In Action series)    Authors=...

Published in 2004
  Title=Hibernate in Action (In Action series)    Authors=...
  Title=Lucene in Action (In Action series)    Authors=...

using the following code:

foreach (var group in groups)
{
  Console.WriteLine("Published in "+group.Year);
  foreach (var book in group.Books)
  {
    Console.Write("  ");
    ObjectDumper.Write(book);
  }
  Console.WriteLine();
}


What a great way to query a catalog of books! Don't you think that this code is very legible and clearly expresses the intention?
Linq to XML can also be used to display the results as XHTML, and Linq to SQL can be used to persist the results in a database.

Over the coming days, I'll publish more details and the complete source code for this example.
Update: The next part of this series is available: Linq to Amazon implementation fore steps
Update: The last part of this series is available: Linq to Amazon source code
Published Monday, June 26, 2006 3:33 PM by Fabrice Marguerie

Comments

 

Steve said:

Can't wait to see the source! :)
June 26, 2006 6:03 PM
 

Alex James said:

That is cool fabrice... well done.
June 27, 2006 4:40 AM
 

Linq in Action News said:

On Monday, I have announced an implementation of Linq for Amazon Web Services, that allows to query for...
June 28, 2006 2:41 PM
 

Fabrice's weblog said:

On Monday, I have announced an implementation of Linq for Amazon Web Services, that allows to query for
June 28, 2006 2:53 PM
 

Wooley's Wonderings said:

Soma just announced some additional re-branding of some of the LINQ stack. In addition to LINQ to SQL...
June 28, 2006 8:07 PM
 

Linq in Action News said:

A while ago, I announced Linq to Amazon and started to describe how it's implemented.&amp;nbsp; Actually...
September 7, 2006 1:23 PM
 

Sébastien Parodi said:

Nice explanation of the Expression Tree

November 24, 2007 2:39 PM
New Comments to this post are disabled