Entity Framework doesn't know how to convert your
CalculateAvgDayFromDates
method to a SQL query.
You'll need to pull the records into memory before performing that calculation.
var managerQuery = from p in db.Projects
let projHOurs = db.ProjectHours.Where(ph => ph.ProjectID == p.ID).Sum(h => h.DayHours)
where (p.ProductionStartDate > sdt && p.ProductionStartDate <= edt)
where (p.ProductionCompleteDate != null && p.ProductType == 1 && p.Active)
select new
{
p.Manager,
p.SalesEmployee,
p.StoreNumber,
p.ReadyToLaunchDate,
p.ProductionStartDate,
projHOurs,
};
var ManagerDetails = from r in managerQuery.AsEnumerable()
select new
{
r.Manager,
r.SalesEmployee,
r.StoreNumber,
LaunchDate = CalculateAvgDayFromDates(r.ReadyToLaunchDate.Value, r.ProductionStartDate.Value),
SoftLaunchDate = CalculateAvgDayFromDates(r.ProductionStartDate.Value.AddDays(6), r.ProductionStartDate.Value),
totalWorkedHours = r.projHOurs,
};
The
AsEnumerable()
call forces the following LINQ methods to be executed in memory, rather than trying to convert them to SQL.