如何组合列表项

本文关键字:列表 组合 何组合 | 更新日期: 2023-09-27 18:02:29

我有一个这样的类:

public class ReportList
    {
        public int? ProjectId { get; set; }
        public string Name { get; set; }
        public string ProjectName { get; set; }
        public int LevelId { get; set; }
        public int Minutes { get; set; }
        public int Hours { get; set; }
        public int ExtraMinutes { get; set; }
        public int ExtraHours { get; set; }
    }

这个类的列表

List<ReportList> repList = new List<ReportList>();

我在列表中添加了项目:

repList.Add(new ReportList(1 , "a" , "project a", 2, 30, 1, 45, 2));
repList.Add(new ReportList(1 , "b" , "project a", 2, 30, 2, 15, 1));
repList.Add(new ReportList(1 , "c" , "project a", 2, 0, 3, 10, 0));

我想把这些列表项按分钟和小时的总和合并成一个项目。所以列表应该是这样的:

{1, "a", "project a", 2, 60, 6, 70, 3};

我能做什么?

如何组合列表项

ProjectIdProjectNameLevelId域上使用GroupBy扩展方法

var results = repList.GroupBy(x=> new {x.ProjectId, x.ProjectName, LevelId })
       .Select(x=> new  // or create new ReportList object.
        {
            ProjectId = x.Key.ProjectId,  
            ProjectName =  x.Key.ProjectName,
            Name = x.First().Name,   // I assume it is first one as per example, modify if you want.
            LevelId = x.Key.LevelId,
            Minutes =  x.Sum(s=>s.Minutes),
            Hours =  x.Sum(s=>s.Hours ),
            ExtraMinutes =  x.Sum(s=>s.ExtraMinutes ),               
            ExtraHours =  x.Sum(s=>s.ExtraHours)                                 
        })
       .ToList() ;

如果您想要用户Hari Prasad发布的答案的更优化版本,您可以使用以下;

        int minuteSum = 0;
        int hoursSum = 0;
        int extraMinutesSum = 0;
        int extraHoursSum = 0;
        foreach (var report in repList)
        {
            minuteSum += report.Minutes;
            hoursSum += report.Hours;
            extraMinutesSum += report.ExtraMinutes;
            extraHoursSum += report.ExtraHours;
        }
        var firstItemInRepList = repList.First();
        var result = new ReportList(firstItemInRepList.ProjectId,
            firstItemInRepList.Name,
            firstItemInRepList.ProjectName,
            firstItemInRepList.LevelId,
            minuteSum,
            hoursSum,
            extraMinutesSum,
            extraHoursSum);

我知道这是更粗糙的版本,但会占用更少的cpu。

var results = repList
   .GroupBy(x => "all")
   .Select(x=> new {
        ProjectId = x.First().ProjectId,
        Name = x.First().Name,   
        ProjectName =  x.First().ProjectName,
        LevelId = x.First().LevelId,
        Minutes =  x.Sum(s=>s.Minutes),
        Hours =  x.Sum(s=>s.Hours ),
        ExtraMinutes =  x.Sum(s=>s.ExtraMinutes),
        ExtraHours = x.Sum(s=>s.ExtraHours)
    });

我参考了用户Hari Prasad发布的答案,但根据问题要求,我猜我们只需要在ProjectId上应用groupby。请参考以下代码。

var processedResult = repList.GroupBy(x => x.ProjectId)
        .Select(x => new ReportList
        {
            ProjectId = x.Key,
            ProjectName = x.First().ProjectName, //As per your example it is first row data
            Name = x.First().Name,   //As per your example it is first row data
            LevelId = x.First().LevelId,
            Minutes = x.Sum(s => s.Minutes),
            Hours = x.Sum(s => s.Hours),
            ExtraMinutes = x.Sum(s => s.ExtraMinutes),
            ExtraHours = x.Sum(s => s.ExtraHours)
        }).ToList();