There are quite a lot of instance where you need to do a primary sort and you need a secondary sort on the same collection and sometimes even multiple sort. With LINQ we can achieve it pretty straight forward.
Let us assume the situation here. We have a list of employees and we need the output to be sorted by city and within every city, we need it to be sorted by the employee's first name. Below is the code which will get your job done.
[sourcecode language="csharp"]
employeeList.OrderBy(item => item.FirstName).OrderBy(item => item.City);
[/sourcecode]
Remember the inner sort / secondary sort has to appear first and the outer sort at the last.
No comments:
Post a Comment