rulururu

post LINQ to Amazon

July 1st, 2009

Filed under: .NET — Kai @ 3:55 pm

As you know you can query with LINQ lots of different sourcse. It’s possible to query, project and filter data in arrays, enumerable classes, XML (XLINQ), relational database, and third party data sources. Last year I among others wrote something about LINQ & XML.

After doing some query we get results of a as a collection of in-memory objects that can be enumerated using a standard iterator function such as C#’s foreach.

I found a funny Provider called 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("darkness and light") &&
    (book.Publisher == "Hitchcock") &&
    (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 var might remind you to the times of Flash, VB or JavaScript. But those times were baaad, very bad. Nowadays we’re strongly typed or even better generic:

A good alternative is:

IEnumerable<SomeOtherClass> results = ...

The returned catalogue can now be grouped, filtered more detailed or just thrown away. Do whatever you like todo ;-)

There are many more, more or less usefull ones:

  • LINQ to SharePoint
  • LINQ to Amazon
  • LINQ to Active Directory (LDAP)
  • LINQ to NHibernate
  • LINQ to MySQL / Oracle / SQLite
  • LINQ to Flickr

Have fun ;)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)