How to sort List records on column1 then on column2 in C#
I have List<UserRoles> and want to sort first on userId and then on Role ID, see following table and required result
// UserRoles table
ID UserId RoleID
1 100 1
2 101 1
3 100 2
4 101 2
// Required result
ID UserId RoleID
1 100 1
2 100 2
3 101 1
4 101 2
2 Answers
you should use OrderBy().ThenBy() to sort your record first or column user id and they by role id
userRolesList.OrderBy(p=>p.UserId)
.ThenBy(q=>q.RoleID)
.ToList();
|
1
|
Answered:
03 Mar 2013
Reputation: 125
|
|
Another way is to use like this:
var resultantList = from p in userRolesList
order by p.userID, p.RoleID
select p;
|
0
|
Answered:
04 Mar 2013
Reputation: 296
|
|
This week users
