How to compare only date components from DateTime in EF?

后端 未结 14 967
野性不改
野性不改 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:35

    //Note for Linq Users/Coders

    This should give you the exact comparison for checking if a date falls within range when working with input from a user - date picker for example:

    ((DateTime)ri.RequestX.DateSatisfied).Date >= startdate.Date &&
            ((DateTime)ri.RequestX.DateSatisfied).Date <= enddate.Date
    

    where startdate and enddate are values from a date picker.

    0 讨论(0)
  • 2020-11-27 17:37

    Just always compare the Date property of DateTime, instead of the full date time.

    When you make your LINQ query, use date.Date in the query, ie:

    var results = from c in collection
                  where c.Date == myDateTime.Date
                  select c;
    
    0 讨论(0)
提交回复
热议问题