我有代码,我按照机会编号对我的要求进行分组,如下所示:
List<Requirement> result = requirements
.GroupBy(l => l.CRMOpportunityNumber)
.Select(cl => new Requirement
{
CRMOpportunityNumber = cl.First().CRMOpportunityNumber,
OpportunityTitle = cl.First().OpportunityTitle,
ClientName = cl.First().ClientName,
TentativeStartDate = cl.Min(c => c.TentativeStartDate),
TotalPositions = cl.Sum(c => c.Required),
RegionName = cl.First().RegionName,
TotalCVProposed = cl.Sum(c => c.Associates.Count),
TotalDeployed = cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true)),
NetGap = cl.Sum(c => c.Required) - cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true)),
}).OrderByDescending(l => l.CRMOpportunityNumber).ToList();
现在我不想在分组(结果)中包含那些要求所有CATEGORY字段值等于“work startedd”的要求。
需求类有一个字段类别。
答案 0 :(得分:4)
在分组前使用Where子句排除'Work Commenced'类别
List<Requirement> result = requirements
.Where(r => r.Category != "work commenced")
.GroupBy(l => l.CRMOpportunityNumber)
.Select(cl => new Requirement
{
CRMOpportunityNumber = cl.First().CRMOpportunityNumber,
OpportunityTitle = cl.First().OpportunityTitle,
ClientName = cl.First().ClientName,
TentativeStartDate = cl.Min(c => c.TentativeStartDate),
TotalPositions = cl.Sum(c => c.Required),
RegionName = cl.First().RegionName,
TotalCVProposed = cl.Sum(c => c.Associates.Count),
TotalDeployed = cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true)),
NetGap = cl.Sum(c => c.Required) - cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true)),
}).OrderByDescending(l => l.CRMOpportunityNumber).ToList();
修改强>
如果要排除所有要求都属于“工作开始”类别的群组,请执行以下操作:
List<Requirement> result = requirements
.GroupBy(l => l.CRMOpportunityNumber)
.Where(cl => cl.All(l => l.Category != "work commenced"))
.Select(cl => new Requirement
{
CRMOpportunityNumber = cl.First().CRMOpportunityNumber,
OpportunityTitle = cl.First().OpportunityTitle,
ClientName = cl.First().ClientName,
TentativeStartDate = cl.Min(c => c.TentativeStartDate),
TotalPositions = cl.Sum(c => c.Required),
RegionName = cl.First().RegionName,
TotalCVProposed = cl.Sum(c => c.Associates.Count),
TotalDeployed = cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true)),
NetGap = cl.Sum(c => c.Required) - cl.Sum(c => c.Associates.Count(x => x.IsIdentified == true))
}).OrderByDescending(l => l.CRMOpportunityNumber)
.ToList();
答案 1 :(得分:0)
如果该组中的所有项目的CATEGORY字段值等于&#34;工作已开始&#34;,则您要排除组。换句话说,如果组中的任何项目的值不是&#34;工作已开始&#34;,则您希望包含该组。这可以使用LINQ Any()
来实现,如下所示:
List<Requirement> result =
requirements.GroupBy(l => l.CRMOpportunityNumber)
.Where(g => g.Any(item => item.Category != "work commenced")
.Select(cl => new Requirement
{
......
}).OrderByDescending(l => l.CRMOpportunityNumber)
.ToList();