by fmarguerie via Fabrice's weblog on 6/26/2006 7:33:00 PM
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 stepsCross-posted from http://linqinaction.net
Original Post: Introducing Linq to Amazon
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.