Ordering

One of the most commonly used query operations in the world of databases is applying ordering to the elements or records. Obviously, LINQ provides for this, as well:

var memoryHungry = from process in Process.GetProcesses()                   orderby process.ProcessName, process.WorkingSet64 descending                   select process;

The preceding query expression translates into the use of two operators, OrderBy and ThenByDescending. Their respective roles will become apparent in just a moment:

var memoryHungry = Process.GetProcesses()                   .OrderBy(process => process.ProcessName)                   .ThenByDescending(process => process.WorkingSet64);

OrderBy and ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.