I have a method which should return a list of anonymous objects with a calculated column like this:
var tomorrow = DateTime.Today.AddDays(1);
return from t in this.Events
where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
select new
{
Client = t.Activity.Project.Customer.Name,
Project = t.Activity.Project.Name,
Task = t.Activity.Task.Name,
Rate = t.Activity.Rate.Name,
StartTime = t.StartTime,
EndTime = t.EndTime.Value,
Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime.Value) / 60),
Description = t.Activity.Description
};
Unfortunately I get the following error from the DateDiff function:
The specified method 'System.Nullable1[System.Int32] DateDiff(System.String, System.Nullable1[System.DateTime], System.Nullable`1[System.DateTime])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.
Any ideas what I could have done wrong here?
EDIT: I also tried the EntityFunctions class mentioned here, but that did not work as well.
Minutes = EntityFunctions.DiffMinutes(t.EndTime, t.StartTime),