Reputation: 11287
I have a DataColumn
of DateTime
datatype and I want to order by the year in descending order followed by the month in descending order.
Here's what I tried:
table.AsEnumerable()
.OrderByDescending(o => o.Field<DateTime>("MaxDateTaken").Year)
.ThenByDescending(o => o.Field<DateTime>("MaxDateTaken").Month)
I get an invalid cast error because both Year
and Month
are int datatypes, but I'm specifying DateTime
in the <>
Any ideas?
Upvotes: 1
Views: 245
Reputation: 3097
Why are you ignoring the day part? It could get simpler:
table = table.AsEnumerable()
.OrderByDescending(o => DateTime.Parse(o.Field<string>("MaxDateTaken")))
Upvotes: 1
Reputation: 11287
I fixed it like this:
table = table.AsEnumerable()
.OrderByDescending(o => DateTime.Parse(o.Field<string>("MaxDateTaken")).Year)
.OrderByDescending(o => DateTime.Parse(o.Field<string>("MaxDateTaken")).Month)
Upvotes: 0
Reputation: 1500275
You shouldn't get a cast error because of that - are you sure that field is really a DateTime
?
It sounds like you really just want
table.AsEnumerable()
.OrderByDescending(o => o.Field<DateTime>("MaxDateTaken"))
anyway, given that ordering by the year and the month descending is basically ordering by the date descending, except it doesn't do anything with the "day" part. Do you really not want to include the "day" part in your ordering?
Upvotes: 3