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.

294 CHAPTER 11 Query expressions and LINQ to ObjectsSo, that’s what the query expression looks like—but what does the compiler do? It simplycalls the OrderBy and ThenBy methods (or OrderByDescend<strong>in</strong>g/ThenBy-Descend<strong>in</strong>g for descend<strong>in</strong>g orders). Our query expression is translated <strong>in</strong>toSampleData.AllDefects.Where (bug => bug.Status != Status.Closed).Where (bug => bug.AssignedTo == tim).OrderByDescend<strong>in</strong>g (bug => bug.Severity).ThenBy (bug => bug.LastModified)Now that we’ve seen an example, let’s look at the general syntax of orderby clauses.They’re basically the contextual keyword orderby followed by one or more order<strong>in</strong>gs.An order<strong>in</strong>g is just an expression (which can use range variables) optionally followed bydescend<strong>in</strong>g, which has the obvious mean<strong>in</strong>g. The translation for the first order<strong>in</strong>g is acall to OrderBy or OrderByDescend<strong>in</strong>g, and any other order<strong>in</strong>gs are translated us<strong>in</strong>g acall to ThenBy or ThenByDescend<strong>in</strong>g, as shown <strong>in</strong> our example.The difference between OrderBy and ThenBy is quite simple: OrderBy assumes ithas primary control over the order<strong>in</strong>g, whereas ThenBy understands that it’s subservientto one or more previous order<strong>in</strong>gs. For LINQ to Objects, ThenBy is only def<strong>in</strong>ed asan extension method for IOrderedEnumerable, which is the type returned byOrderBy (and by ThenBy itself, to allow further cha<strong>in</strong><strong>in</strong>g).It’s very important to note that although you can use multiple orderbyclauses, each one will start with its own OrderBy or OrderByDescend<strong>in</strong>gclause, which means the last one will effectively “w<strong>in</strong>.” There may be somereason for <strong>in</strong>clud<strong>in</strong>g multiple orderby clauses, but it would be veryunusual. You should almost always use a s<strong>in</strong>gle clause conta<strong>in</strong><strong>in</strong>g multipleorder<strong>in</strong>gs <strong>in</strong>stead.As noted <strong>in</strong> chapter 10, apply<strong>in</strong>g an order<strong>in</strong>g requires all the data tobe loaded (at least for LINQ to Objects)—you can’t order an <strong>in</strong>f<strong>in</strong>itesequence, for example. Hopefully the reason for this is obvious—youdon’t know whether you’ll see someth<strong>in</strong>g that should come at the start of the result<strong>in</strong>gsequence until you’ve seen all the elements, for example.We’re about halfway through learn<strong>in</strong>g about query expressions, and you may be surprisedthat we haven’t seen any jo<strong>in</strong>s yet. Obviously they’re important <strong>in</strong> LINQ just asthey’re important <strong>in</strong> SQL, but they’re also complicated. I promise we’ll get to them <strong>in</strong>due course, but <strong>in</strong> order to <strong>in</strong>troduce just one new concept at a time, we’ll detour vialet clauses first. That way we can learn about transparent identifiers before we hit jo<strong>in</strong>s.Warn<strong>in</strong>g:only use oneorderbyclause!11.4 Let clauses and transparent identifiersMost of the rest of the operators we still need to look at <strong>in</strong>volve transparent identifiers.Just like range variables, you can get along perfectly well without understand<strong>in</strong>g transparentidentifiers, if you only want to have a fairly shallow grasp of query expressions.If you’ve bought this book, I hope you want to know <strong>C#</strong> 3 at a deeper level, which will(among other th<strong>in</strong>gs) enable you to look compilation errors <strong>in</strong> the face and knowwhat they’re talk<strong>in</strong>g about.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!