如何组合列表项

时间:2016-08-16 08:34:27

标签: c# linq list

我是这样的一个班级:

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};

我该怎么办?

4 个答案:

答案 0 :(得分:1)

GroupByProjectIdProjectName字段上使用LevelId扩展方法。

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

答案 1 :(得分:1)

如果您想要用户 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。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

我正在引用用户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();