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 change15Personally, I f<strong>in</strong>d the earlier list<strong>in</strong>g easier to read—the only benefit to the queryexpression version is that the where clause is simpler.So if query expressions are no good, why is everyone mak<strong>in</strong>g such a fuss aboutthem, and about LINQ <strong>in</strong> general? The first answer is that while query expressions arenot particularly suitable for simple tasks, they’re very, very good for more complicatedsituations that would be hard to read if written out <strong>in</strong> the equivalent method calls(and fiendish <strong>in</strong> <strong>C#</strong> 1 or 2). Let’s make th<strong>in</strong>gs just a little harder by <strong>in</strong>troduc<strong>in</strong>ganother type—Supplier. I haven’t <strong>in</strong>cluded the whole code here, but complete readyto-compilecode is provided on the book’s website (www.csharp<strong>in</strong>depth.com). We’llconcentrate on the fun stuff.Each supplier has a Name (str<strong>in</strong>g) and a SupplierID (<strong>in</strong>t). I’ve also addedSupplierID as a property <strong>in</strong> Product and adapted the sample data appropriately.Admittedly that’s not a very object-oriented way of giv<strong>in</strong>g each product a supplier—it’s much closer to how the data would be represented <strong>in</strong> a database. It makes thisparticular feature easier to demonstrate for now, but we’ll see <strong>in</strong> chapter 12 thatLINQ allows us to use a more natural model too.Now let’s look at the code (list<strong>in</strong>g 1.15) to jo<strong>in</strong> the sample products with the samplesuppliers (obviously based on the supplier ID), apply the same price filter asbefore to the products, sort by supplier name and then product name, and pr<strong>in</strong>t outthe name of both supplier and product for each match. That was a mouthful (f<strong>in</strong>gerful?)to type, and <strong>in</strong> earlier versions of <strong>C#</strong> it would have been a nightmare to implement.In LINQ, it’s almost trivial.List<strong>in</strong>g 1.15Jo<strong>in</strong><strong>in</strong>g, filter<strong>in</strong>g, order<strong>in</strong>g, and project<strong>in</strong>gList products = Product.GetSampleProducts();List suppliers = Supplier.GetSampleSuppliers();var filtered = from p <strong>in</strong> productsjo<strong>in</strong> s <strong>in</strong> 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);}The more astute among you will have noticed that it looks remarkably like SQL. 2Indeed, the reaction of many people on first hear<strong>in</strong>g about LINQ (but before exam<strong>in</strong><strong>in</strong>git closely) is to reject it as merely try<strong>in</strong>g to put SQL <strong>in</strong>to the language for the sakeof talk<strong>in</strong>g to databases. Fortunately, LINQ has borrowed the syntax and some ideasfrom SQL, but as we’ve seen, you needn’t be anywhere near a database <strong>in</strong> order to use2If you’ve ever worked with SQL <strong>in</strong> any form whatsoever but didn’t notice the resemblance, I’m shocked.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!