I have this LINQ query. The date is stored as a string in the database, but I need to order by it. So I convert it to a DateTime, but it doesn't order.
(from m in dbDataContext.TimeCards
where m.TIMECARDDATE != ""
&& m.TIMECARDDATE != null
orderby Convert.ToDateTim开发者_Go百科e(m.TIMECARDDATE) descending
select Convert.ToDateTime(m.TIMECARDDATE)).Distinct().ToList();
Any idea why it doesn't work? I can't change the database, so I have to deal with the data the way it is. I get the data back ordered like this...
2/27/2009
2/26/2009
2/25/2009
2/28/2009
2/24/2009
Try evaluating the expression without sorting, and then apply the sort to the results of Distinct:
(Linq-To-Sql-Expression).Distinct().OrderByDescending(x => x.TIMECARDDATE).ToList()
First you may use string.IsNullOrEmpty()
, instead of checking whether the string is null or ""
Beside, this should work. You probably reorder it outside of this code.
Pay attention this isn't execute until you foreach it (call GetEnumerator()
).
So if you change something before iterating it.. that might be the reason.
p.s. is TIMECARDDATE is object or string?
精彩评论