Attempting to subtract two dates from one another to figure out the number of days, then execute .Average()
on the 'let' variable avgConversion
.
I encounter the following error; LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression.
var averageConversion =
(
from r in db.Registrations
where
(bu == "All" || r.BusinessUnit.Equals(bu)) &&
(region == "All" || r.Region.Equals(region)) &&
(startDate == null || r.StartDate >= startDate) &&
(endDate == null || r.EndDate <= endDate) &&
!r.RegistrationStatus.Equals("Cancelled") &&
!r.Status.Equals("Cancelled")
let avgConversion = r.StartDate.Value.Subtract(r.RegistrationDate.Value).Days
select avgConversion
).Average();
Thanks to Enigma, this is what ended up solving the problem.
var dates = (from r in db.Registrations
where
(bu == "All" || r.BusinessUnit.Equals(bu)) &&
(region == "All" || r.Region.Equals(region)) &&
(startDate == null || r.StartDate >= startDate) &&
(endDate == null || r.EndDate <= endDate) &&
!r.RegistrationStatus.Equals("Cancelled") &&
!r.Status.Equals("Cancelled")
select new
{
r.RegistrationDate,
r.StartDate
}).ToList();
var avgConversion = (from d in dates
let AvgConversion = d.StartDate.Value.Subtract(d.RegistrationDate.Value).Days
select AvgConversion).Average();