How to get only Date from datetime column using linq

女生的网名这么多〃 提交于 2019-12-23 12:37:21

问题


I have a column in the database as EffectiveDate

which is the type of DateTime.

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

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

how to write the Linq query for that?

Thanks


回答1:


Call the Date property on any DateTime struct

EffectiveDate.Date;

or call

EffectiveDate.ToShortDateString();

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

EffectiveDate.ToString("d");

Writing a Linq query could look like this:

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

and if EffectiveDate is nullable you can try:

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.



来源:https://stackoverflow.com/questions/5747732/how-to-get-only-date-from-datetime-column-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!