How to compare only date components from DateTime in EF?

后端 未结 14 1010
野性不改
野性不改 2020-11-27 17:06

I am having two date values, one already stored in the database and the other selected by the user using DatePicker. The use case is to search for a particular date from the

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 17:20

    This is how I do this.

    DateTime date_time_to_compare = DateTime.Now;
    //Compare only date parts
    context.YourObject.FirstOrDefault(r =>
                    EntityFunctions.TruncateTime(r.date) == EntityFunctions.TruncateTime(date_to_compare));
    

提交回复
热议问题