LINQ删除组by中具有特定值的项

  • 本文关键字:删除 by LINQ c# linq group-by
  • 更新时间 :
  • 英文 :


我有代码,我按机会号分组我的需求,如下所示:

        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字段值等于"工作开始"的需求。

需求类有一个字段类别

在分组前使用Where子句排除'Work started '类别

    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();

如果该组中的所有项的CATEGORY字段值等于"work started ",则要排除该组。换句话说,如果组中的任何项具有"工作开始"以外的值,则需要包含该组。这可以使用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();

相关内容

  • 没有找到相关文章

最新更新