Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DateTime.AddDays(x) in Entity Framework

I have this code:

from pr in e.ProgramSetup.Include("Program").Include("Program.Client")
        where pr.DateBegin < DateTime.Now
        && pr.DateEnd > DateTime.Now.AddDays(pr.DateEndOffset) 
select pr).ToList();

It does not work, because AddDays() is not possible to use for generating sql.

So is there some another way? Now i select everything and filter it finaly by foreach, but it is not good way in my opinion.

Problem is that pr.DateEndOffset is also only in db, it is not constant...

like image 695
Zdenek G Avatar asked Oct 01 '12 13:10

Zdenek G


People also ask

How to add 1 day to date in C#?

AddDays() method in C# is used to add the specified number of days to the value of this instance. This method returns a new DateTime.

How to add 10 days to current date in C#?

Firstly, get the current date. Now, use AddDays() method to add days to the current date. Here, we are adding 10 days to the current date.

How to add date Time now in C#?

You can use the DateTime. Add() method to add the time to the date. DateTime date = DateTime. Now; TimeSpan time = new TimeSpan(36, 0, 0, 0); DateTime combined = date.


1 Answers

using System.Data.Entity;
...
DbFunctions.AddDays(dateFromDataStore, numDaysToAdd);
like image 65
Graham Laight Avatar answered Sep 25 '22 00:09

Graham Laight