Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to sum

Tags:

c#

linq

i'm having problem summing items in a modular way.

I have this class:

public enum ReportType
{
  Worker,
  Management,
  Guest
}

public class Report
{
  public ReportType Type { get; set; }
  public int Lost { get; set; }

  public Report(ReportType typ, int los)
  {
     Type = typ;
     Lost = los;
  }
}

and a method that gets a list of Report, and should find out and return a list of Report with the biggest loss, plus some calculation according to that number. i started the method:

public class ReportsManager
{
  public static List<Report> CalculateBiggerLost(List<Report> reportList)
  {
     int WorkerSum = 0;
     int ManagementSum = 0;
     int GuestSum = 0;

     foreach (Report report in reportList)
     {
        switch (report.Type)
        {
              case ReportType.Guest:
              {
                 GuestSum += report.Lost;
                 break;
              }
              case ReportType.Management:
              {
                 ManagementSum += report.Lost;
                 break;
              }
              case ReportType.Worker:
              {
                 WorkerSum += report.Lost;
                 break;
              }
        }
     }

     if (GuestSum >= ManagementSum && GuestSum >= WorkerSum)
     {
        // Doing some report
     }
     else if (WorkerSum >= ManagementSum)
     {
        // inner Check
     }
  }
}

but it's not good in case i'll might have another value in ReportType enum and then will have to rewrite the code, plus it's not so readable. i'm new to linq but i started to try and GroupBy using linq but got stuck when i got and answer to reportList.GroupBy(x => x.Type); and did not know what to do with it, as in what's the structure? how do i iterate it?

like image 587
No Idea For Name Avatar asked Jan 28 '26 12:01

No Idea For Name


2 Answers

This is the query that you want:

var results = reportList.GroupBy(r => r.Type)
                        .ToDictionary(g => g.Key, g => g.Sum(r => r.Lost));
int WorkerSum = results[ReportType.Worker];
int ManagementSum = results[ReportType.Management];
int GuestSum = results[ReportType.Guest];

This will work fine if you expect to have at least one report of each type. If it's possible that there are no reports of a give type you'd have to check for that first, like this:

int WorkerSum = results.ContainsKey(ReportType.Worker) ? results[ReportType.Worker] : 0;
int ManagementSum = results.ContainsKey(ReportType.Management) ? results[ReportType.Management] : 0;
int GuestSum = results.ContainsKey(ReportType.Guest) ? results[ReportType.Guest] : 0;

Or like this:

int WorkerSum;
int ManagementSum;
int GuestSum;
results.TryGetValue(ReportType.Worker, out WorkerSum);
results.TryGetValue(ReportType.Management, out ManagementSum);
results.TryGetValue(ReportType.Guest, out GuestSum);
like image 104
p.s.w.g Avatar answered Jan 31 '26 02:01

p.s.w.g


int guestSum = reportList.Where(x=>x.Type==ReportType.Guest).Sum(x=>x.Lost);
int workerSum = reportList.Where(x=>x.Type==ReportType.Worker).Sum(x=>x.Lost);
int managementSum = reportList.Where(x=>x.Type==ReportType.Manment).Sum(x=>x.Lost);
like image 23
Ehsan Avatar answered Jan 31 '26 02:01

Ehsan