Accepted answer

you shouldn't to try order the date as string;

var res = objui.where(x => x.lastlogin != null)
    .groupby(x =>
    .orderbydescending(x => x.key)
    .select(x => new { lastlogin = string.format("{0:mm/dd/yyyy}", x.key) })


you're ordering by your string representation which starts with the month.

it looks like you're actually only interested in distinct dates though - grouping and then discarding everything other than the key is equivalent to just projecting and then taking distinct data. i'd also suggest avoiding an anonymous type unless you really need it - an anonymous type with only a single property is usually a bad idea.

finally, if you're formatting a single value - certainly a datetime value - it's simpler to just call the tostring method. in the code below i've explicitly specified the invariant culture too - otherwise you may find an unexpected calendar system is used...

so i'd write:

var res = objui.where(x => x.lastlogin != null)
               .select(x => x.lastlogin)
               .orderbydescending(x => x)
               .select(x => x.tostring("mm/dd/yyyy", cultureinfo.invariantculture))

Related Query

More Query from same tag