Is a collection of objects of some kind this code

Info icon This preview shows pages 300–302. Sign up to view the full content.

View Full Document Right Arrow Icon
is a collection of objects of some kind, this code will ensure that controls is an enumeration that only returns objects that can be cast to the Control type. Although OfType has no equivalent in a query expression, that doesn’t stop you from using it in conjunction with a query expression—you can use the result of OfType as the source for a query: var controlNames = from control in myPanel.Children.OfType<Control>() where !string.IsNullOrEmpty(control.Name) select control.Name; This uses the OfType operator to filter the items down to objects of type Control , and then uses a where clause to further filter the items to just those with a nonempty Name property. Ordering Query expressions can contain an orderby clause, indicating the order in which you’d like the items to emerge from the query. In queries with no orderby clause, LINQ does not, in general, make any guarantees about the order in which items emerge. LINQ to Objects happens to return items in the order in which they emerge from the source enumeration if you don’t specify an order, but other LINQ providers will not necessarily define a default order. (In particular, database LINQ providers typically return items in an unpredictable order unless you explicitly specify an order.) So as to have some data to sort, Example 8-10 brings back the CalendarEvent class from Chapter 7 . Example 8-10. Class representing a calendar event class CalendarEvent { public string Title { get; set; } public DateTimeOffset StartTime { get; set; } public TimeSpan Duration { get; set; } } When examples in this chapter refer to an events variable, assume that it was initialized with the data shown in Example 8-11 . Example 8-11. Some example data List<CalendarEvent> events = new List<CalendarEvent> { new CalendarEvent { Title = "Swing Dancing at the South Bank", StartTime = new DateTimeOffset (2009, 7, 11, 15, 00, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(4) }, new CalendarEvent { 276 | Chapter 8: LINQ
Image of page 300

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

View Full Document Right Arrow Icon
Title = "Saturday Night Swing", StartTime = new DateTimeOffset (2009, 7, 11, 19, 30, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(6.5) }, new CalendarEvent { Title = "Formula 1 German Grand Prix", StartTime = new DateTimeOffset (2009, 7, 12, 12, 10, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(3) }, new CalendarEvent { Title = "Swing Dance Picnic", StartTime = new DateTimeOffset (2009, 7, 12, 15, 00, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(4) }, new CalendarEvent { Title = "Stompin' at the 100 Club", StartTime = new DateTimeOffset (2009, 7, 13, 19, 45, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(5) } }; Example 8-12 shows a LINQ query that orders these events by start time. Example 8-12. Ordering items with LINQ var eventsByStartTime = from ev in events orderby ev.StartTime select ev; By default, the items will be sorted into ascending order. You can be explicit about this if you like: var eventsByStartTime = from ev in events orderby ev.StartTime ascending select ev; And, of course, you can sort into descending order too: var eventsByStartTime = from ev in events orderby ev.StartTime descending select ev; The expression in the orderby clause does not need to correspond directly to a property of the source object. It can be a more complex expression. For example, we could
Image of page 301
Image of page 302
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