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.

Filter<strong>in</strong>g and order<strong>in</strong>g a sequence293List<strong>in</strong>g 11.8Sort<strong>in</strong>g by the severity of a bug, from high to low priorityUser tim = SampleData.Users.TesterTim;var query = from bug <strong>in</strong> SampleData.AllDefectswhere bug.Status != Status.Closedwhere bug.AssignedTo == timorderby bug.Severity descend<strong>in</strong>gselect bug;foreach (var bug <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1}", bug.Severity, bug.Summary);}The output of list<strong>in</strong>g 11.8 shows that we’ve sorted the results appropriately:Showstopper: Webcam makes me look baldMajor: Subtitles only work <strong>in</strong> WelshMajor: Play button po<strong>in</strong>ts the wrong wayM<strong>in</strong>or: Network is saturated when play<strong>in</strong>g WAV fileTrivial: Installation is slowHowever, you can see that we’ve got two major defects. Which order should those betackled <strong>in</strong>? Currently no clear order<strong>in</strong>g is <strong>in</strong>volved. Let’s change the query so thatafter sort<strong>in</strong>g by severity <strong>in</strong> descend<strong>in</strong>g order, we sort by “last modified time” <strong>in</strong> ascend<strong>in</strong>gorder. This means that Tim will test the bugs that have been fixed a long time agobefore the ones that were fixed recently. This just requires an extra expression <strong>in</strong> theorderby clause, as shown <strong>in</strong> list<strong>in</strong>g 11.9.List<strong>in</strong>g 11.9Order<strong>in</strong>g by severity and then last modified timeUser tim = SampleData.Users.TesterTim;var query = from bug <strong>in</strong> SampleData.AllDefectswhere bug.Status != Status.Closedwhere bug.AssignedTo == timorderby bug.Severity descend<strong>in</strong>g, bug.LastModifiedselect bug;foreach (var bug <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1} ({2:d})",bug.Severity, bug.Summary, bug.LastModified);}The results of list<strong>in</strong>g 11.9 are shown here. Note how the order of the two majordefects has been reversed.Showstopper: Webcam makes me look bald (08/27/2007)Major: Play button po<strong>in</strong>ts the wrong way (08/17/2007)Major: Subtitles only work <strong>in</strong> Welsh (08/23/2007)M<strong>in</strong>or: Network is saturated when play<strong>in</strong>g WAV file (08/31/2007)Trivial: Installation is slow (08/15/2007)Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!