LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method

后端 未结 4 464
野性不改
野性不改 2020-12-03 06:44

I try to select records in database in 60 days 30 days 20 days differents in current date.

Please see this query in below.

 var uploads = (
                  


        
4条回答
  •  难免孤独
    2020-12-03 07:18

    To add on to scartag's answer,

    The MSDN documentation for DbFunctions.DiffDays doesn't directly mention whether and when the value returned by DiffDays() will be negative, so I thought I'd provide that information here:

    The result will be negative when the argument 1 date is larger than (i.e. in the future relative to) the argument 2 date.

    For example, given a table Deliveries with a non-null field ScheduledDeliveryDate that can have values both in the past and in the future relative to the current date, this query will get all records with a delivery date/time within 2 days of the current date/time (both past and future):

    DateTime now = DateTime.Now;
    var results = from d in Deliveries
        where (DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) < 2
            && DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) > -2)
        select d;
    

提交回复
热议问题