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.

Evolution <strong>in</strong> action: examples of code change17LINQ to SQL know about what to expect <strong>in</strong> what table, but it’s all fairly straightforward.List<strong>in</strong>g 1.17 shows the query<strong>in</strong>g code.List<strong>in</strong>g 1.17Apply<strong>in</strong>g a query expression to a SQL databaseus<strong>in</strong>g (L<strong>in</strong>qDemoDataContext db = new L<strong>in</strong>qDemoDataContext()){var filtered = from p <strong>in</strong> db.Productsjo<strong>in</strong> s <strong>in</strong> db.Supplierson p.SupplierID equals s.SupplierIDwhere p.Price > 10orderby s.Name, p.Nameselect new{SupplierName = s.Name,ProductName = p.Name};foreach (var v <strong>in</strong> filtered){Console.WriteL<strong>in</strong>e("Supplier={0}; Product={1}",v.SupplierName, v.ProductName);}}By now, this should be look<strong>in</strong>g <strong>in</strong>credibly familiar. Everyth<strong>in</strong>g below the“jo<strong>in</strong>” l<strong>in</strong>e is cut and pasted directly from list<strong>in</strong>g 1.14 with no changes.That’s impressive enough, but if you’re performance conscious you maybe wonder<strong>in</strong>g why we would want to pull down all the data from the databaseand then apply these .NET queries and order<strong>in</strong>gs. Why not get thedatabase to do it? That’s what it’s good at, isn’t it? Well, <strong>in</strong>deed—andthat’s exactly what LINQ to SQL does. The code <strong>in</strong> list<strong>in</strong>g 1.17 issues adatabase request, which is basically the query translated <strong>in</strong>to SQL. Eventhough we’ve expressed the query <strong>in</strong> <strong>C#</strong> code, it’s been executed as SQL.We’ll see later that the way this query jo<strong>in</strong>s isn’t how we’d normally use LINQ to SQL—there’s a more relation-oriented way of approach<strong>in</strong>g it when the schema and the entitiesknow about the relationship between suppliers and products. The result is thesame, however, and it shows just how similar LINQ to Objects (the <strong>in</strong>-memory LINQoperat<strong>in</strong>g on collections) and LINQ to SQL can be.It’s important to understand that LINQ is flexible, too: you can write your ownquery translators. It’s not easy, but it can be well worth it. For <strong>in</strong>stance, here’s an exampleus<strong>in</strong>g Amazon’s web service to query its available books:Query iswritten <strong>in</strong> <strong>C#</strong>,but executesas SQLvar query =from book <strong>in</strong> new L<strong>in</strong>qToAmazon.AmazonBookSearch()wherebook.Title.Conta<strong>in</strong>s("ajax") &&(book.Publisher == "Mann<strong>in</strong>g") &&(book.Price

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

Saved successfully!

Ooh no, something went wrong!