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.

Group<strong>in</strong>gs and cont<strong>in</strong>uations307In just five l<strong>in</strong>es of code we have retrieved, parsed, and filtered a whole collection oflog files, return<strong>in</strong>g a sequence of entries represent<strong>in</strong>g errors. Crucially, we haven’t hadto load even a s<strong>in</strong>gle full log file <strong>in</strong>to memory all <strong>in</strong> one go, let alone all of the files—all the data is streamed.Hav<strong>in</strong>g tackled jo<strong>in</strong>s, the last items we need to look at are slightly easier to understand.We’re go<strong>in</strong>g to look at group<strong>in</strong>g elements by a key, and cont<strong>in</strong>u<strong>in</strong>g a queryexpression after a group … by or select clause.11.6 Group<strong>in</strong>gs and cont<strong>in</strong>uationsOne common requirement is to group a sequence of elements by one of its properties.LINQ makes this easy with the group … by clause. As well as describ<strong>in</strong>g this f<strong>in</strong>altype of clause, we’ll also revisit our earliest one (select) to see a feature called querycont<strong>in</strong>uations that can be applied to both group<strong>in</strong>gs and projections. Let’s start with asimple group<strong>in</strong>g.11.6.1 Group<strong>in</strong>g with the group … by clauseGroup<strong>in</strong>g is largely <strong>in</strong>tuitive, and LINQ makes it simple. To group a sequence <strong>in</strong> aquery expression, all you need to do is use the group … by clause, with this syntax:group projection by group<strong>in</strong>gThis clause comes at the end of a query expression <strong>in</strong> the same way a select clausedoes. The similarities between these clauses don’t end there: the projection expressionis the same k<strong>in</strong>d of projection a select clause uses. The outcome is somewhat different,however.The group<strong>in</strong>g expression determ<strong>in</strong>es what the sequence is grouped by—the key ofthe group<strong>in</strong>g. The overall result is a sequence where each element is itself a sequenceof projected elements, and also has a Key property, which is the key for that group;this comb<strong>in</strong>ation is encapsulated <strong>in</strong> the IGroup<strong>in</strong>g <strong>in</strong>terface, whichextends IEnumerable.Let’s have a look at a simple example from the SkeetySoft defect system: group<strong>in</strong>gdefects by their current assignee. List<strong>in</strong>g 11.17 does this with the simplest form of projection,so that the result<strong>in</strong>g sequence has the assignee as the key, and a sequence ofdefects embedded <strong>in</strong> each entry.List<strong>in</strong>g 11.17Group<strong>in</strong>g defects by assignee—trivial projectionvar query = from defect <strong>in</strong> SampleData.AllDefectswhere defect.AssignedTo != nullgroup defect by defect.AssignedTo;foreach (var entry <strong>in</strong> query){Console.WriteL<strong>in</strong>e(entry.Key.Name);foreach (var defect <strong>in</strong> entry){Console.WriteL<strong>in</strong>e(" ({0}) {1}",Uses key of eachentry: the assigneeEDBFilters outunassigned defectsGroups byassigneeCIterates over entry’ssubsequenceLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!