2011-04-21 54 views
4

我在数据库中有一列为EffectiveDate如何从日期时间列使用LINQ的日期

这是DateTime的类型。

我有一个linq查询来从表中得到EffectiveDate。但是当我得到时,我随着时间的推移获得了EffectiveDate。

但在我的linq查询中,我只需要日期我不想为EffectiveDate的时间。

如何编写Linq查询呢?

感谢

+1

你使用的是实体框架吗? – 2011-04-21 17:44:56

回答

11

调用Date财产上的任何DateTime结构

EffectiveDate.Date; 

或致电

EffectiveDate.ToShortDateString(); 

或致电ToString()多个DateTime格式here时,使用 “d” 格式。

EffectiveDate.ToString("d"); 

编写LINQ查询看起来是这样的:

someCollection.Select(i => i.EffectiveDate.ToShortDateString()); 

如果EffectiveDate可为空,你可以尝试:

someCollection 
    .Where(i => i.EffectiveDate.HasValue) 
    .Select(i => i.EffectiveDate.Value.ToShortDateString()); 

DateTime.Date Property

与此实例具有相同日期并且时间值设置为午夜12:00:00(00:00:00)的时间值的新对象。

DateTime.ToShortDateString Method

包含当前DateTime对象的短日期字符串表示的字符串。

+1

我无法调用DateTime.Date。其投掷运行时间错误。 – user300485 2011-04-21 17:41:25

+0

@user究竟哪个错误? – 2011-04-21 17:41:58

+2

@user如果您使用的是Entity框架,那么您就对EDM.DateTime没有.Date属性。查看[EDM.DateTime](http://msdn.microsoft.com/en-us/library/bb387157.aspx)了解您可以使用哪些方法。 – Chad 2011-04-21 18:31:32

相关问题