is_oz
is_oz

Reputation: 973

How to add hour from another column to date in Linq to SQL

I write that code. I want to use T-SQL DATEADD.

var result = dbContext.MyEntity
.Where(DbFunctions.AddHours(g.Date,g.Hour) >= minDate 
&& DbFunctions.AddHours(g.Date,g.Hour) <= maxDate).ToList();

When this gets to SQL, I get the error:

The datepart hour is not supported by date function dateadd for data type date

Upvotes: 0

Views: 765

Answers (1)

is_oz
is_oz

Reputation: 973

I found solution, may be help someone else;

var result = dbContext.MyEntity
.Where(DbFunctions.CreateDateTime(g.Date.Year,g.Date.Month,g.Date.Day,g.Hour,0,0) >= minDate 
&& DbFunctions.CreateDateTime(g.Date.Year,g.Date.Month,g.Date.Day,g.Hour,0,0) <= maxDate).ToList();

Upvotes: 2

Related Questions