0

I have a list that I need sorted by two fields. I'm looking for the list to be sorted by the first field and then, if there are any null in the first field, to sort by the second field.
I have two field for example Distance and from time and I want a sorted list with both fields. But I want to give priority to from time and then distance so how can I sort the list using Linq?

Steve
  • 203,265
  • 19
  • 210
  • 265
  • Please include an example of expected results. It's not really clear what you mean by _"sorted by the first field and then if there are any null in the first field to sort by the second field"_. – Chris Pickford Mar 15 '16 at 10:25
  • 1
    Is this what you're looking for? http://stackoverflow.com/questions/298725/multiple-order-by-in-linq – RobotG0d Mar 15 '16 at 10:27

1 Answers1

2

You can use many orderBy closes like this:

from user in users
orderby user.Name, user.Age, user.Name.Length
select user;

Or you can do something like this:

OrderBy(<sort 1>).ThenBy(<sort 2>)
S. Nadezhnyy
  • 584
  • 2
  • 6