Sort by second field if the first field is null

How to convert this in C# linq/lambda query

select *
from meetings
order by ISNULL(ActualStartDate, StartDate) desc

so far I tried this but seems like it doesn't work:

meetings.OrderByDescending(m => m.ActualStartDate ?? m.StartDate);

UPDATE:

Actually this is correct. The problem is on the listview that shows the item. Apologies.

Actually my real problem is when both ActualStartDate and StartDate is null and I want it to show last. But thats a separate question I guess.

Jon Skeet
people
quotationmark

I suspect you want:

var sorted =  meetings.OrderByDescending(m => m.ActualStartDate ?? m.StartDate);

Note that calling the method won't change meetings - you need to use the return value which will be a sorted sequence of results. (This is in line with how LINQ works in general.)

If that still doesn't work, and assuming this is LINQ to SQL or something similar, you should look at the generated SQL to work out what's going on... then you can try to adjust your query appropriately.

people

See more on this question at Stackoverflow