尝试将两个日期相减以计算出天数,然后.Average()
在 'let' 变量上执行avgConversion
。
我遇到以下错误;LINQ to Entities 无法识别方法 'System.TimeSpan Subtract(System.DateTime)' 方法,并且此方法无法转换为存储表达式。
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();
感谢 Enigma,这最终解决了这个问题。
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();