2

I have the following method that is used to filter an ObjectQuery by date using EF:

public List<T> FilterObjectSetByDate<T>(ObjectQuery<T> inputQuery, string dateColumn) where T : class
{
    ObjectQuery<T> filteredQuery = inputQuery.Where("(dateadd(dd,0, datediff(dd,0, it" + dateColumn + ")) = @p0)");
    ObjectParameter objParam = new ObjectParameter("p0", DateTime.Now);
    filteredQuery.Parameters.Add(objParam);

    return filteredQuery.ToList();
}      

Which leads to the following error:

'dateadd' cannot be resolved into a valid type or function.

Is there any way to return a list of records for a given day (ignoring the time portion of the DateTime) using Entity Framework?

woggles
  • 7,444
  • 12
  • 70
  • 130

2 Answers2

1

Ok figured this out this morning:

Where("SqlServer.datediff('DAY'," + dateColumn + ", @p{1}) = 0")
woggles
  • 7,444
  • 12
  • 70
  • 130
0

Googling suggests using "SqlServer.dateadd... instead of just "dateadd..., similarly for datediff.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Thanks for pointing me in the right direction, but it won't allow the use of an int as the second argument of SqlServer.datediff...looking for a workaround now – woggles Sep 04 '12 at 13:45
  • Hah thanks... I was just going to delete this since you found the answer anyway, but I'll accept a small amount of credit for getting you started :) – Rawling Sep 05 '12 at 08:19
  • yeah...was gonna upvote when I accepted my answer anyway :) downvote totally undeserved! – woggles Sep 05 '12 at 08:20