Quick Tip: LINQ OrderBy and ThenBy

The other day I required ordering a list of people first by the date they were created, and then by their last name. I spent forever trying to find out why my two OrderBy statements were conflicting with one another, until I remembered you use “ThenBy()” for any additional sorting you want to do.

So the query looked something like this then:

IEnumerable<Person> myPersons = myDataContext.Persons
.OrderBy(m=>m.DateCreated)
.ThenBy(m=>m.LastName);

 

Just thought I’d share! It’s easy to forget all the useful LINQ methods, like I did.

Print | posted on Wednesday, July 29, 2009 3:56 PM

Feedback

# re: Quick Tip: LINQ OrderBy and ThenBy

left by Supersum at 10/29/2009 4:13 PM Gravatar
Thanks for for the Info Samer
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: