4

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

user300485
  • 525
  • 5
  • 11
  • 24

1 Answers1

12

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.

hunter
  • 62,308
  • 19
  • 113
  • 113
  • 1
    I am not able to call DateTime.Date. its throwing run time Error. – user300485 Apr 21 '11 at 17:41
  • 2
    @user If you are using Entity framework then you are right the EDM.DateTime does not have a .Date property. Check out [EDM.DateTime](http://msdn.microsoft.com/en-us/library/bb387157.aspx) for what methods you can use. – Chad Apr 21 '11 at 18:31
  • @Chad it's tagged `linq-to-sql` but if that's not what the OP is using then you're correct – hunter Apr 21 '11 at 18:34