It can be a more complex expression for example we

Info icon This preview shows pages 301–303. Sign up to view the full content.

View Full Document Right Arrow Icon
of the source object. It can be a more complex expression. For example, we could extract just the time of day to produce the slightly confusing result of events ordered by what time they start, regardless of date: var eventsByStartTime = from ev in events orderby ev.StartTime.TimeOfDay select ev; You can specify multiple criteria. Example 8-13 sorts the events: first by date (ignoring the time) and then by duration. LINQ Operators | 277
Image of page 301

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

View Full Document Right Arrow Icon
Example 8-13. Multiple sort criteria var eventsByStartDateThenDuration = from ev in events orderby ev.StartTime.Date, ev.Duration select ev; Four LINQ query operator methods correspond to the orderby clause. Most obviously, there’s OrderBy , which takes a single ordering criterion as a lambda: var eventsByStartTime = events.OrderBy(ev => ev.StartTime); That code has exactly the same effect as Example 8-12 . Of course, like most LINQ operators, you can chain this together with other ones. So we could combine that with the Where operator: var longEvents = events.OrderBy(ev => ev.StartTime). Where(ev => ev.Duration > TimeSpan.FromHours(2)); This is equivalent to the following query: var longEvents = from ev in events orderby ev.StartTime where ev.Duration > TimeSpan.FromHours(2) select ev; You can customize the comparison mechanism used to sort the items by using an over- load that accepts a comparison object—it must implement IComparer<TKey> where TKey is the type returned by the ordering expression. So in these examples, it would need to be an IComparer<DateTimeOffset> , since that’s the type of the StartTime prop- erty we’re using to order the data. There’s not a lot of scope for discussion about what order dates come in, so this is not a useful example for plugging in an alternate com- parison. However, string comparisons do vary a lot—different languages have different ideas about what order letters come in, particularly when it comes to letters with ac- cents. The .NET Framework class library offers a StringComparer class that can provide an IComparer<string> implementation for any language and culture supported in .NET. The following example uses this in conjunction with an overload of the OrderBy oper- ator to sort the events by their title, using a string sorting order appropriate for the French-speaking Canadian culture, and configured for case insensitivity: CultureInfo cult = new CultureInfo("fr-CA"); // 2nd argument is true for case insensitivity StringComparer comp = StringComparer.Create(cult, true); var eventsByTitle = events.OrderBy(ev => ev.Title, comp); There is no equivalent query expression—if you want to use anything other than the default comparison for a type, you must use this overload of the OrderBy operator. † This is very similar to IComparable<T> , introduced in the preceding chapter. But while objects that implement IComparable<T> can themselves be compared with other objects of type T , an IComparer<T> compares two objects of type T —the objects being compared are separate from the comparer.
Image of page 302
Image of page 303
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