This is a quick one.
In Linq to sql, I need to order by a datetime field descending then order by the time part of the same datetime filed ascending, make sense? Here's an example of the result needed:
[Serial] [Date (mm-dd-yyyy:hh-MM-ss)]
1 3-13-2008:10-10-02
2 3-13-2008:10-12-60
3 3-12-2008:12-05-55
I tried :
return query.OrderByDescending(c=> c.Time).ThenBy(c=> c.Time.TimeOfDay);
And also tried:
query.OrderByDescending(c => c.Time).ThenBy(c => c.Time.Hour).ThenBy(c=> c.Time.Minute).ThenBy(c=> c.Time.Second);
but never seemed to work.
Any suggestions?
Use the Date
and TimeOfDay
properties for the sort.
return query
.OrderByDescending(c=> c.Time.Date)
.ThenBy(c=> c.Time.TimeOfDay);
The Problem is that "Time" contains Date and Time information, thus, already sorting completely. You would have to take the Date Part to sort with first.
return query.OrderByDescending(c=> c.Time.Date).ThenBy(c=> c.Time.TimeOfDay);
Not tested :)
I think first orderByDescending sorts everything by datetime(date+time), so the following orderbys will become effective only if you have any records with the same timestamp.
Try this
query.OrderByDescending(c=> c.Time.Date).ThenBy(c=> c.Time.TimeOfDay)
Turn it to ticks will work as well:
return query.OrderByDescending(c=> c.Time.Ticks)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With