Fabrice's weblog

Tools and Source


My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET


ASP.NET Hosting transatlantys




Selected content


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()
    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("  ");

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

Cross-posted from http://linqinaction.net


ScottGu said:

Very cool!  

Please send me a link to your sample source once it is posted.  I'd love to link to it! :-)


# June 26, 2006 2:44 PM

Keith J. Farmer said:

You beat me to it :)
# June 26, 2006 3:33 PM