1

In order for explanation purposes... let's say I have a table that is generated by a table in my database..

This table holds 3 pieces of information, FlightDate, Aircraft, Pre / Post.

So, the logic behind this is that there should be a pre-flight and a post-flight per day.

Now, for clarity, let's say the table so far looks like this:

  |Date|    |    |Aircraft|    |    |Pre / Post|

 04/08/2016 |     aircraft1    |      Pre-Flight
 04/08/2016 |     aircraft3    |      Pre-Flight
 04/08/2016 |     aircraft2    |      Pre-Flight
 04/08/2016 |     aircraft3    |      Post-Flight
 04/08/2016 |     aircraft1    |      Post-Flight
 04/08/2016 |     aircraft2    |      Post-Flight

As you can see there is no order by that table (except for maybe the date, but I just made this up).

Now what I would like to happen is when the page loads to display this table, for the table to be ordered by the Aircraft and it's Pre / Post based on the Date...

so essentially what I am looking for:

  |Date|     |    |Aircraft|    |    |Pre / Post|

 04/08/2016  |     aircraft1    |     Pre-Flight
 04/08/2016  |     aircraft1    |     Post-Flight
 04/08/2016  |     aircraft2    |     Pre-Flight
 04/08/2016  |     aircraft2    |     Post-Flight
 04/08/2016  |     aircraft3    |     Pre-Flight
 04/08/2016  |     aircraft3    |     Post-Flight
 04/09/2016  |     aircraft1    |     Pre-Flight
 04/09/2016  |     aircraft1    |     Post-Flight
 etc         |     etc          |     etc

Is there a way to use the .OrderBy method to achieve this outcome?

Farside
  • 8,130
  • 2
  • 41
  • 55
Grizzly
  • 5,411
  • 5
  • 39
  • 94

4 Answers4

7

You can chain together multiple OrderBy() calls by following the first one with ThenBy() or ThenByDescending() respectively :

// This would order by FlightDate, Aircraft then PrePost
var aircraftFlights = context.AircraftFlights.OrderBy(a => a.FlightDate)
                                             .ThenBy(a => a.Aircraft)
                                             .ThenBy(a => a.PrePost);

Since you want your Pre-flight value to always precede your Post-flight values, you might consider explicitly using an OrderByDescending() for that column (as Pre- comes after Post- alphabetically) :

// Consider using ThenByDescending to get your Pre/Post ordering correct
.ThenBy(a => a.PrePost);

You can tailor this to suit your needs. Basically once you call your first OrderBy() call, your collection becomes an OrderedEnumerable which exposes the ThenBy() and ThenByDescending() calls and will handle chaining them together.

Rion Williams
  • 69,631
  • 35
  • 180
  • 307
2

You can use OrderBy followed by ThenBy calls for each ordering constraint you want to apply. Assuming your enumerable you are ordering has all three properties it may look like this:

myEnumerable.OrderBy(x => x.Aircraft).ThenBy(z => z.PrePost).ThenBy(a => a.Date);
davidallyoung
  • 1,204
  • 13
  • 14
2

You need to combine OrderBy and ThenBy using a lambda to evaluate the Pre/Post column. So something like this:

var orderedTable = table.OrderBy(t => t.Aircraft).
    ThenBy(t => t.Date).
    ThenBy(t => t.PrePost == "Post-Flight" ? 1 : 0);
René Vogt
  • 40,163
  • 14
  • 65
  • 85
1

Use the ThenBy extension of System.LinQ.

Your code might look like this:

var ordered = myFlights.OrderBy(f => f.Aircraft)
                       .ThenBy(f => f.prePost)
                       .ThenBy(f => f.Date);

For reference please check MSDN documentation

Toxantron
  • 2,098
  • 8
  • 22
  • Not only is this basically a direct rip from the previous answer by DavidY, like David's answer, the ordering here is entirely wrong. – Chris Pratt Apr 11 '16 at 15:19
  • I only saw his post after I send mine and to be fair all answers are identical except for the order and I admit I misinterpreted that one. – Toxantron Apr 11 '16 at 15:22