I have a table "Orders" with the following columns:
- Id
- Created (Date)
- Modified (Date)
An order always has Created date but might have or not have Modified date. Here is an example
- 1, 2019-12-24, null
- 2, 2019-03-13, 2020-01-10
- 3, 2019-02-10, 2019-02-25
- 4, 2019-01-10, null
How can I create a query that will order the orders chronologically by last interaction with them? In the example above the result should be 4, 3, 1, 2?
If I do orders.OrderBy(o => o.Modified).ThenBy(o => o.Created)
the nulls will be 4, 1, 3, 2 because of the nulls. If I ignore them using first OrderBy(o => o.Modified.HasValue)
then those entities will be put either in the beginning or in the end.
CodePudding user response:
Try this approach:
orders.OrderBy(o => o.Modified ?? o.Created);