LINQ-To-SQL OrderBy与DateTime不起作用 [英] LINQ-To-SQL OrderBy with DateTime Not Working

查看:284
本文介绍了LINQ-To-SQL OrderBy与DateTime不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个LINQ查询.日期以字符串形式存储在数据库中,但是我需要通过它进行排序.因此,我将其转换为DateTime,但没有排序.

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.ToDateTime(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

推荐答案

尝试对表达式进行排序而不进行排序,然后将排序应用于Distinct的结果:

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()

这篇关于LINQ-To-SQL OrderBy与DateTime不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆