How to get only Date from datetime column using li

2019-06-27 13:27发布

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条回答
2楼-- · 2019-06-27 13:47

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.

查看更多
登录 后发表回答