如何使用linq从datetime列中仅获取Date [英] How to get only Date from datetime column using linq

查看:157
本文介绍了如何使用linq从datetime列中仅获取Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一个列,称为有效日期(EffectiveDate)

I have a column in the database as EffectiveDate

这是DateTime的类型.

which is the type of DateTime.

我有一个linq查询,用于从表中获取有效日期.但是当我得到时,我就会随着时间的推移获得EffectiveDate.

I have a linq query to get EffectiveDate from the table. but when I get I am getting EffectiveDate with time.

但是在我的linq查询中,我只需要Date(日期),我就不需要时间来启用EffectiveDate.

But in my linq query i need only Date I dont want the time for EffectiveDate.

如何为此编写Linq查询?

how to write the Linq query for that?

谢谢

推荐答案

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

EffectiveDate.Date;

或致电

EffectiveDate.ToShortDateString();

或在调用ToString() 更多DateTime格式时使用"d"格式此处.

or use the "d" format when calling ToString() more DateTime formats here.

EffectiveDate.ToString("d");


编写Linq查询可能看起来像这样:


Writing a Linq query could look like this:

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

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

and if EffectiveDate is nullable you can try:

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


DateTime.Date属性

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

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方法

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

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

这篇关于如何使用linq从datetime列中仅获取Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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