Skipping a column that is part of GROUP BY statement in nested foreach-loops?

46 Views Asked by At

I'm adding data to a SQL database (thousands of rows), aggregating the columns, and then using the processed data to build invoices (tens of rows per invoice).

I GROUP BY three columns:

GROUP BY [KjøretøyID], [LøyvehaverID], [Ordrenummer]

[Ordrenummer] is a distinct, unique number for each row. I need to group by [KjøretøyID] (vehicle) and [LøyvehaverID] (company), which means that each [LøyvehaverID] end up with 2-10 rows each. But since [Ordrenummer] is part of the GROUP BY as well, they end up with several hundreds each (non-aggregated rows).

So, in my C# code, my question is: Is there any way I can skip [Ordrenummer] when I am nesting foreach loops?

IEnumerable<IGrouping<String, PaidTrip>> tripsGroupedByCompany = paidTrips.GroupBy(pt => pt.LicenseHolderID);

foreach (IGrouping<String, PaidTrip> companyGroup in tripsGroupedByCompany) {

    // code

    var groupedByVehicle = companyGroup.GroupBy(t => t.VehicleID);
    
    foreach (IGrouping<String, PaidTrip> vehicleGroup in groupedByVehicle) {
    
    // code

        foreach (PaidTrip trip in vehicleGroup) {

        // this is where there's an overflow. I'm supposed to have 2-10 rows, but get hundreds (the non-aggregated total)
        
        }
    }
}
0

There are 0 best solutions below