根据登录用户的说法,我必须总结来自服务的数据。到目前为止,我已经达到了这个目的:
SummaryModel q = (from s in pe.Services
join g in pe.Groups on s.GroupId equals g.GroupId
join ud in pe.UserDetails on s.UserIdInCharge equals ud.UserId into ud1
from ud in ud1.DefaultIfEmpty()
where allowedGroups.Contains(g.GroupId)
&& (!filterBySector || filter.selectedSectors.Contains(g.GroupId))
&& (!filterByGroup || (s.UserIdInCharge.HasValue
&& members.Contains(s.UserIdInCharge.Value)))
&& (s.Status == (decimal)Data.Enums.ServiceStatus.Active
|| s.Status == (decimal)Data.Enums.ServiceStatus.Sold)
&& s.ServiceStartDate >= startDate
&& s.ServiceStartDate <= endDate
group s by new Guid() into services
select new SummaryModel()
{
Holes = services.Where(ss => ss.UserIdInCharge == null).Count(),
Candidates = services.Sum(ss => ss.ServiceApplies.Count()),
Announced = services.Where(ss => ss.AnnouncementType != 0).Count(),
TotalServices = services.Count(),
TotalHours = (decimal)services.Sum(ss => SqlFunctions
.DateDiff("hh", ss.ServiceStartDate, ss.ServiceEndDate)),
TotalBudget = services.Sum(ss => ss.Value.Value),
}).SingleOrDefault();
但下面的行返回:
TotalHours = (decimal)services.Sum(ss => SqlFunctions.DateDiff("hh",
ss.ServiceStartDate, ss.ServiceEndDate)),
错误
LINQ to Entities无法识别 “System.Nullable
1[System.Int32] DateDiff(System.String, System.Nullable
1的System.DateTime] System.Nullable`1 [System.DateTime])',方法,它不能转换为 存储库的表达。
日期样本
StartDate =&gt; 05/09/2011 23:00:00
EndDate =&gt; 06/09/2011 11:00:00
任何替代方案?
答案 0 :(得分:1)
也许是这样的
var timeDifArray = (from s in pe.Services
join g in pe.Groups on s.GroupId equals g.GroupId
join ud in pe.UserDetails on s.UserIdInCharge equals ud.UserId into ud1
from ud in ud1.DefaultIfEmpty()
where allowedGroups.Contains(g.GroupId)
&& (!filterBySector || filter.selectedSectors.Contains(g.GroupId))
&& (!filterByGroup || (s.UserIdInCharge.HasValue
&& members.Contains(s.UserIdInCharge.Value)))
&& (s.Status == (decimal)Data.Enums.ServiceStatus.Active
|| s.Status == (decimal)Data.Enums.ServiceStatus.Sold)
&& s.ServiceStartDate >= startDate
&& s.ServiceStartDate <= endDate
group s by new Guid() into services
select new
{
services.ServiceStartDate,
services.ServiceEndDate,
}).ToArray();
var timeDifs = timeDifArray.Select(x => x.ServiceEndDate.Subtract(x.ServiceEndDate));
var totalTimeDifs = timDifs.Sum(x => x.Hours);
答案 1 :(得分:0)
实际上发生的事情是VS正在使用System.Data.Entity.SqlServer.SqlFunctions
代替System.Data.Objects.SqlClient.SqlFunctions
。我强制命名空间,以便LINQ可以找到正确的函数集。