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.

312 CHAPTER 11 Query expressions and LINQ to Objectsvar tmp = from defect <strong>in</strong> SampleData.AllDefectswhere defect.AssignedTo != nullgroup defect by defect.AssignedTo;var query = from grouped <strong>in</strong> tmpselect new { Assignee=grouped.Key,Count=grouped.Count() };Of course, if you f<strong>in</strong>d this easier to read there’s noth<strong>in</strong>g to stop you from break<strong>in</strong>g upthe orig<strong>in</strong>al expression <strong>in</strong>to this form <strong>in</strong> your source code. Noth<strong>in</strong>g will be evaluateduntil you start try<strong>in</strong>g to step through the query results anyway, due to deferred execution.Let’s extend this example to see how multiple cont<strong>in</strong>uations can be used. Ourresults are currently unordered—let’s change that so we can see who’s got the mostdefects assigned to them first. We could use a let clause after the first cont<strong>in</strong>uation,but list<strong>in</strong>g 11.20 shows an alternative with a second cont<strong>in</strong>uation after our currentexpression.List<strong>in</strong>g 11.20Query expression cont<strong>in</strong>uations from group and selectvar query = from defect <strong>in</strong> SampleData.AllDefectswhere defect.AssignedTo != nullgroup defect by defect.AssignedTo <strong>in</strong>to groupedselect new { Assignee=grouped.Key,Count=grouped.Count() } <strong>in</strong>to resultorderby result.Count descend<strong>in</strong>gselect result;foreach (var entry <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1}",entry.Assignee.Name,entry.Count);}The changes between list<strong>in</strong>g 11.19 and 11.20 are highlighted <strong>in</strong> bold. We haven’t hadto change any of the output code as we’ve got the same type of sequence—we’ve justapplied an order<strong>in</strong>g to it. This time the translated query expression is as follows:SampleData.AllDefects.Where (defect => defect.AssignedTo != null).GroupBy(defect => defect.AssignedTo).Select(grouped => new { Assignee=grouped.Key,Count=grouped.Count() }).OrderByDescend<strong>in</strong>g(result => result.Count);By pure co<strong>in</strong>cidence, this is remarkably similar to the first defect track<strong>in</strong>g query wecame across, <strong>in</strong> section 10.3.5. Our f<strong>in</strong>al select clause effectively does noth<strong>in</strong>g, so the<strong>C#</strong> compiler ignores it. It’s required <strong>in</strong> the query expression, however, as all queryexpressions have to end with either a select or a group … by clause. There’s noth<strong>in</strong>gto stop you from us<strong>in</strong>g a different projection or perform<strong>in</strong>g other operations with thecont<strong>in</strong>ued query—jo<strong>in</strong>s, further group<strong>in</strong>gs, and so forth. Just keep an eye on the readabilityof the query expression as it grows.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!