这是我的LINQ当前的样子
var regionResults = (
from p in _context.Projects
from pr in p.Regions
where (data.RegionId == null || pr.RegionId == data.RegionId)
group p by pr.RegionId into g
join q in _context.Regions on g.Key equals _context.Regions.First().Id
select new Models.ViewModels.ProjectBreakdownViewModel.Regions
{
RegionName = q.Name,
TotalCount = g.Count(),
RejectedCount = g.Count(e => e.SubmissionStatusId == 2),
DeniedCount = g.Count(e => e.SubmissionStatusId == 3)
});
这就是我想要的...
我知道问题出在本质上
join q in _context.Regions on g.Key equals _context.Regions.First().Id
我不知道如何不使用.First()来执行此操作,似乎没有一种方法可以执行此操作。我很接近,我只是不知道该如何完成。
答案 0 :(得分:1)
如果您的ProjectRegions
实体中有Region
的集合,则可以执行以下操作:
var result= context.Regions
.Where(r=> data.RegionId == null || r.Id == data.RegionId)
.Select(r=> new
{
RegionName = r.Name,
TotalCount = r.ProjectRegions.Count(),
RejectedCount = r.ProjectRegions.Count(e => e.Project.SubmissionStatusId == 2),
DeniedCount = r.ProjectRegions.Count(e => e.Project.SubmissionStatusId == 3)
});
ProjectRegion
实体应具有两个导航属性Project
和Region
,使用它们导航并创建相应的条件