13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

308 CHAPTER 11 Query expressions and LINQ to Objects}}Console.WriteL<strong>in</strong>e();defect.Severity,defect.Summary);List<strong>in</strong>g 11.17 might be useful <strong>in</strong> a daily build report, to quickly see what defects eachperson needs to look at. We’ve filtered out all the defects that don’t need any moreattention B and then grouped us<strong>in</strong>g the AssignedTo property. Although this timewe’re just us<strong>in</strong>g a property, the group<strong>in</strong>g expression can be anyth<strong>in</strong>g you like—it’s justapplied to each entry <strong>in</strong> the <strong>in</strong>com<strong>in</strong>g sequence, and the sequence is grouped basedon the result of the expression. Note that group<strong>in</strong>g cannot stream the results,although it streams the <strong>in</strong>put, apply<strong>in</strong>g the key selection and projection to each elementand buffer<strong>in</strong>g the grouped sequences of projected elements.The projection we’ve applied <strong>in</strong> the group<strong>in</strong>g C is trivial—it just selects the orig<strong>in</strong>alelement. As we go through the result<strong>in</strong>g sequence, each entry has a Key property,which is of type User D, and each entry also implements IEnumerable,which is the sequence of defects assigned to that user E.The results of list<strong>in</strong>g 11.17 start like this:Darren Dahlia(Showstopper) MP3 files crash system(Major) Can't play files more than 200 bytes long(Major) DivX is choppy on Pentium 100(Trivial) User <strong>in</strong>terface should be more caramellyAfter all of Darren’s defects have been pr<strong>in</strong>ted out, we see Tara’s, then Tim’s, and soon. The implementation effectively keeps a list of the assignees it’s seen so far, andadds a new one every time it needs to. Figure 11.9 shows the sequences generatedthroughout the query expression, which may make this order<strong>in</strong>g clearer.With<strong>in</strong> each entry’s subsequence, the order of the defects is the same as the orderof the orig<strong>in</strong>al defect sequence. If you actively care about the order<strong>in</strong>g, considerexplicitly stat<strong>in</strong>g it <strong>in</strong> the query expression, to make it more readable.If you run list<strong>in</strong>g 11.17, you’ll see that Mary Malcop doesn’t appear <strong>in</strong> the output atall, because she doesn’t have any defects assigned to her. If you wanted to produce afull list of users and defects assigned to each of them, you’d need to use a group jo<strong>in</strong>like the one used <strong>in</strong> list<strong>in</strong>g 11.14.The compiler always uses a method called GroupBy for group<strong>in</strong>g clauses. When theprojection <strong>in</strong> a group<strong>in</strong>g clause is trivial—<strong>in</strong> other words, when each entry <strong>in</strong> the orig<strong>in</strong>alsequence maps directly to the exact same object <strong>in</strong> a subsequence—the compileruses a simple method call, which just needs the group<strong>in</strong>g expression, so it knows howto map each element to a key. For <strong>in</strong>stance, the query expression <strong>in</strong> list<strong>in</strong>g 11.17 istranslated <strong>in</strong>to this nonquery expression:SampleData.AllDefects.Where(defect => defect.AssignedTo != null).GroupBy(defect => defect.AssignedTo)Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!