加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使用linq从datetime列获取Date

发布时间:2020-12-16 03:57:49 所属栏目:asp.Net 来源:网络整理
导读:我在数据库中有一列作为EffectiveDate 这是DateTime的类型. 我有一个linq查询从表中获取EffectiveDate.但是当我得到我的时候会得到EffectiveDate. 但在我的linq查询中,我只需要Date,我不想要有效时间. 如何编写Linq查询? 谢谢 解决方法 在任何DateTime结构
我在数据库中有一列作为EffectiveDate

这是DateTime的类型.

我有一个linq查询从表中获取EffectiveDate.但是当我得到我的时候会得到EffectiveDate.

但在我的linq查询中,我只需要Date,我不想要有效时间.

如何编写Linq查询?

谢谢

解决方法

在任何DateTime结构上调用Date属性

EffectiveDate.Date;

或者打电话

EffectiveDate.ToShortDateString();

或者在调用ToString()时使用“d”格式,更多DateTime格式为here.

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

A new object with the same date as this instance,and the time value set to 12:00:00 midnight (00:00:00).

DateTime.ToShortDateString Method

A string that contains the short date string representation of the current DateTime object.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读