How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?

后端 未结 3 579
后悔当初
后悔当初 2020-11-27 15:51

If I have a Linq to SQL expression like this:

  from subscription in dbContext.Subscriptions
 where subscription.Expires > DateT         


        
3条回答
  •  萌比男神i
    2020-11-27 16:10

    If you don't mind querying the database before every use, I would suggest the following workaround: Use ExecuteQuery in one place to get the date in the data context like this:

    public partial class YourDataContext
    {
      public DateTime GetDate()
      {
        return ExecuteQuery("SELECT GETDATE()").First();
      }
    }
    

    and then you can write

    from subscription in dbContext.Subscriptions
    where subscription > dbContext.GetDate().AddDays(2)
    select subscription
    

提交回复
热议问题