Example 8 15 simple linq grouping var eventsbyday

Info icon This preview shows pages 304–307. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 8-15. Simple LINQ grouping var eventsByDay = from ev in events group ev by ev.StartTime.Date; This will arrange the objects in the events source into one group for each day. The eventsByDay variable here ends up with a slightly different type than anything we’ve seen before. It’s an IEnumerable<IGrouping<DateTimeOffset, CalendarEvent>> . So eventsByDay is an enumeration, and it returns an item for each group found by the group clause. Example 8-16 shows one way of using this. It iterates through the collec- tion of groupings, and for each grouping it displays the Key property—the value by which the items have been grouped—and then iterates through the items in the group. Example 8-16. Iterating through grouped results foreach (var day in eventsByDay) { Console.WriteLine("Events for " + day.Key); foreach (var item in day) { Console.WriteLine(item.Title); } } This produces the following output: Events for 7/11/2009 12:00:00 AM Swing Dancing at the South Bank Saturday Night Swing Events for 7/12/2009 12:00:00 AM Formula 1 German Grand Prix Swing Dance Picnic Events for 7/13/2009 12:00:00 AM Stompin' at the 100 Club This illustrates that the query in Example 8-15 has successfully grouped the events by day, but let’s look at what returned in a little more detail. Each group is represented as an IGrouping<TKey, TElement> , where TKey is the type of the expression used to group the data—a DateTimeOffset in this case—and TElement is the type of the elements making up the groups— CalendarEvent in this example. IGrouping<TKey, TElement> derives from IEnumerable<TElement> , so you can enumerate through the contents of a group like you would any other enumeration. (In fact, the only thing IGrouping<TKey, TElement> adds is the Key property, which is the grouping value.) So the query in Ex- ample 8-15 returns a sequence of sequences—one for each group (see Figure 8-1 ). 280 | Chapter 8: LINQ
Image of page 304

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
While a LINQ query expression is allowed to end with a group clause, as Exam- ple 8-15 does, it doesn’t have to finish there. If you would like to do further processing, you can add an into keyword on the end, followed by an identifier. The continuation of the query after a group ... into clause will iterate over the groups, and the identifier effectively becomes a new range variable. Example 8-17 uses this to convert each group into an array. (Calling ToArray on an IGrouping effectively discards the Key , and leaves you with just an array containing that group’s contents. So this query ends up producing an IEnumerable<CalendarEvent[]> —a collection of arrays.) Figure 8-1. Result of groupby query LINQ Operators | 281
Image of page 305
Example 8-17. Continuing a grouped query with into var eventsByDay = from ev in events group ev by ev.StartTime.Date into dayGroup select dayGroup.ToArray(); Like the ordering operators, grouping will cause LINQ to Objects to evaluate the whole source sequence before returning any results.
Image of page 306

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 307
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern