Beep beep
Beep beep

Reputation: 19151

OrderByDescending on list of DateTime's not working?

In a lambda expression, how do you order by a list of DateTime values, rather than an object? For example, I want to get the most recent 3 pay dates on the following query:

 var hires = (from e in md.Employee
             where e.HireDate.Year == 2011
             select e.HireDate).Distinct();

 //at this point hires =
   6/3/2011
   5/15/2011
   6/1/2011
   7/1/2011

My assumption was that I could do .OrderByDescending(x => x).Take(3), but when I do so I don't get 7/1/2011 (just the first 3 above). It's like the OrderByDescending is being ignored.

What am I doing wrong?

Upvotes: 3

Views: 2987

Answers (1)

Christian Payne
Christian Payne

Reputation: 7169

Are you sure you are putting the orderby & distinct in the right order?

Try:

var hires = (from e in md.Employee
    where e.HireDate.Year == 2011
    select e.HireDate).Distinct().OrderByDescending(e => e);

Upvotes: 3

Related Questions