Lukas
Lukas

Reputation: 109

arithmetic value using DateTime and TimeSpan net core 2.0 and EF

I have DateTime variables

 private readonly TimeSpan thirtydays = new TimeSpan(30, 0, 0, 0);
    public DateTime RegistrationDate { get; set; }
    public DateTime DueDate { get; set; }

and TimeSpan used to calculate date in the future

process.DueDate = DateTime.Now + thirtydays;

Due date can be changed during the process life. I would like to list all processes where DueDate was changed.

.Where(d => d.DueDate != (d.RegistrationDate + thirtydays))

but I get an error

InvalidCastException: Failed to convert parameter value from a TimeSpan to a DateTime.

System.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__108_0(Task<SqlDataReader> result)

Upvotes: 4

Views: 558

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109185

You can use

.Where(d => d.DueDate != d.RegistrationDate.AddDays(30))

In Entity Framework core this is translated into the SQL DATEADD function.

Upvotes: 3

Related Questions