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.

310 CHAPTER 11 Query expressions and LINQ to Objects{}Console.WriteL<strong>in</strong>e(entry.Key.Name);foreach (var summary <strong>in</strong> entry){Console.WriteL<strong>in</strong>e(" {0}", summary);}Console.WriteL<strong>in</strong>e();I’ve highlighted the differences between list<strong>in</strong>g 11.18 and list<strong>in</strong>g 11.17 <strong>in</strong> bold. Hav<strong>in</strong>gprojected a defect to just its summary, the embedded sequence <strong>in</strong> each entry is just anIEnumerable. In this case, the compiler uses an overload of GroupBy withanother parameter to represent the projection. The query expression <strong>in</strong> list<strong>in</strong>g 11.18is translated <strong>in</strong>to the follow<strong>in</strong>g expression:SampleData.AllDefects.Where(defect => defect.AssignedTo != null).GroupBy(defect => defect.AssignedTo,defect => defect.Summary)There are more complex overloads of GroupBy available as extension methods onIEnumerable, but they aren’t used by the <strong>C#</strong> 3 compiler when translat<strong>in</strong>g queryexpressions. You can call them manually, of course—if you f<strong>in</strong>d you want more powerfulgroup<strong>in</strong>g behavior than query expressions provide natively, then they’re worthlook<strong>in</strong>g <strong>in</strong>to.Group<strong>in</strong>g clauses are relatively simple but very useful. Even <strong>in</strong> our defect-track<strong>in</strong>gsystem, you could easily imag<strong>in</strong>e want<strong>in</strong>g to group defects by project, creator, severity,or status, as well as the assignee we’ve used for these examples.So far, we’ve ended each query expression with a select or group … by clause, andthat’s been the end of the expression. There are times, however, when you want to domore with the results—and that’s where query cont<strong>in</strong>uations are used.11.6.2 Query cont<strong>in</strong>uationsQuery cont<strong>in</strong>uations provide a way of us<strong>in</strong>g the result of one query expression as the<strong>in</strong>itial sequence of another. They apply to both group … by and select clauses, andthe syntax is the same for both—you simply use the contextual keyword <strong>in</strong>to and thenprovide the name of a new range variable. That range variable can then be used <strong>in</strong> thenext part of the query expression.The <strong>C#</strong> 3 specification expla<strong>in</strong>s this <strong>in</strong> terms of a translation from one queryexpression to another, chang<strong>in</strong>g<strong>in</strong>tofirst-query <strong>in</strong>to identifiersecond-query-bodyfrom identifier <strong>in</strong> (first-query)second-query-bodyAn example will make this a lot clearer. Let’s go back to our group<strong>in</strong>g of defects byassignee, but this time imag<strong>in</strong>e we only want the count of the defects assigned to eachLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!