13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LINQ beyond .NET 3.5345List<strong>in</strong>g 12.19Query<strong>in</strong>g Amazon’s web service with LINQAmazonBookSearch source = new AmazonBookSearch();var webQuery = from book <strong>in</strong> sourcewhere book.Title.Conta<strong>in</strong>s("LINQ")select book;CCreates providerB with your access IDCreates queryfor web servicevar query = from book <strong>in</strong> webQuery.AsEnumerable()orderby book.Year, book.Titleselect book;DPerforms moreoperations <strong>in</strong> memoryforeach (AmazonBook book <strong>in</strong> query){Console.WriteL<strong>in</strong>e ("{0}: {1}", book.Year, book.Title);}I’ve taken Fabrice’s provider and tweaked it slightly so that we can pass our own AmazonAccess ID <strong>in</strong>to the provider’s constructor B. The source is part of the Visual Studio2008 solution conta<strong>in</strong><strong>in</strong>g the examples for this chapter.You may be slightly surprised to see two query expressions <strong>in</strong> list<strong>in</strong>g 12.19. As aproof of concept, the LINQ to Amazon provider only allows a limited number ofoperations, not <strong>in</strong>clud<strong>in</strong>g order<strong>in</strong>g. We use the web query C as the source for an<strong>in</strong>-memory LINQ to Objects query expression D. The web service call still only takesplace when we start execut<strong>in</strong>g the query E, due to the deferred execution approachtaken by LINQ.The output at the time of this writ<strong>in</strong>g is <strong>in</strong>cluded here. By the time you read this, Iexpect the list may be considerably longer.1998: A l<strong>in</strong>q between nonwovens and wovens. (...)2007: Introduc<strong>in</strong>g Microsoft LINQ2007: LINQ for VB 20052007: LINQ for Visual <strong>C#</strong> 20052007: Pro LINQ: Language Integrated Query <strong>in</strong> <strong>C#</strong> 20082008: Beg<strong>in</strong>n<strong>in</strong>g ASP.NET 3.5 Data Access with LINQ , <strong>C#</strong> (...)2008: Beg<strong>in</strong>n<strong>in</strong>g ASP.NET 3.5 Data Access with LINQ, VB (...)2008: LINQ <strong>in</strong> Action2008: Professional LINQE Executesquery,displaysresultsEven though LINQ to Amazon is primitive, it demonstrates an important po<strong>in</strong>t: LINQis capable of more than just database and <strong>in</strong>-memory queries. Our next providerproves that even when it’s talk<strong>in</strong>g to databases, there’s more to LINQ than just LINQto SQL.LINQ TO NHIBERNATENHibernate is an open source ORM framework for .NET, based on the Hibernateproject for Java. It supports textual queries <strong>in</strong> its own query language (HQL) and alsoa more programmatic way of build<strong>in</strong>g up queries—the Criteria API.Prolific blogger and NHibernate contributor Ayende 6 has <strong>in</strong>itiated a LINQ to NHibernateprovider that converts LINQ queries not <strong>in</strong>to SQL but <strong>in</strong>to NHibernate Criteriaqueries, tak<strong>in</strong>g advantage of the SQL translation code <strong>in</strong> the rest of the project. Aside6www.ayende.com/Blog/Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!