Reputation: 1933
I've a class like this:
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; }
}
And I've list of this class
List<ReportList> repList = new List<ReportList>();
I've added items to this list:
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));
I want to combine this list items into one item by sum minutes and hours. So the list should be like this:
{1, "a", "project a", 2, 60, 6, 70, 3};
What can I do?
Upvotes: 0
Views: 105
Reputation: 410
I am refering answer posted by user Hari Prasad, but as per question requirement we need to apply groupby only on ProjectId i guess. Please refer below code.
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();
Upvotes: 1
Reputation: 2128
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)
});
Upvotes: 1
Reputation: 1505
If you want more optimized version of answer posted by user Hari Prasad you could use following;
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);
I know its more crude version but it will take less cpu.
Upvotes: 1
Reputation: 16986
Use GroupBy
extension method on ProjectId
,ProjectName
and LevelId
fields.
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() ;
Upvotes: 1