Arturo Martinez Arturo Martinez - 4 months ago 51
C# Question

Ordering LinQ c#

im trying to order my LINQ by the date value showing first today's date and the counting down but in my actual LINQ the values are not arranged they all come sometimes ordered by day and sometimes by month but never from today's date

This is my LINQ:

foreach (var item in db.VENTA_PLATILLOS.Select(l => l.Fecha)
.Distinct().OrderByDescending(x => x.Month == result.Month && x.Day == result.Day))
dateday = item.ToString("yyyy-MM-dd");
var listItem = new SelectListItem { Value = dateday, Text = dateday };
listItem.Selected = result.Day == item.Day;

How can i order them correctly by actual date first?


Values comes from a Datetime Model Variable i wil add some examples of dates:

2016-11-06 00:00:00.000
2016-11-05 00:00:00.000
2016-11-04 00:00:00.000
2016-11-03 00:00:00.000
2016-11-02 00:00:00.000
2016-10-31 00:00:00.000
2016-10-29 00:00:00.000

like this

EDIT 2 *****

Ordering by Month and then by day finally worked after making some changes to my model

        .OrderByDescending(x => x.Month)
        .ThenByDescending(x => x.Day)

LINQ offers ThenByDescending which will allow you to order after an initial order.

But you should be able to just do this

.OrderByDescending(x => x) because you're already iterating over a datetime.