Multiple Sort For Collection Using LINQ - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Saturday, May 28, 2011

Multiple Sort For Collection Using LINQ

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.

Detail...

No comments:

Post a Comment