C#C
C#4y ago
dino2dy

❔ Linq group by with multiple columns for the outer and then 1 other column for the inner group

Lets say I have a list of class Car. It has properties Id, Model, Year, Age,CarCode. A carCode is unique for a model but is not for all models.

I want to group by Model and Year together and then group that grouping by Age and then for each Agegroup in each model and year group send Model, Age, and list<string>CarCodes to a method which will update a db table with those CarCodes.

I found some pieces of code online but couldnt get anything to give me the thing I need.
  var grouped = bla
  .GroupBy(l => new { l.sreSif, l.sreSerija})//group by two things
   .Select(y => new { Element = y.Key,   })
  .GroupBy(f => f.Element.sreSerija)//this will become the outer grouping

//THEN I TRIED 
var consolidatedChildren =
    from c in bla
    group c by new
    {
       
        c.sreSif,
        c.sreSerija,
    } into gcs
    from g in gcs
    group g by new
    {
     g.sreIsplatio
    };

this line of thinking did not work
What I want
var carModelYearGroupList=GroupBy CarLIst(Model, Year)
var carAgeGL= groupBy carModelYearGroupList(key.Age)

Parallel.ForEach(carModelYearGroupList, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (car) =>
           {
Parallel.ForEach(carAgeGL, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (ca) =>
           {
              UpdateDb(Model m, Age a, List<string> carCodes); 
});
});
Was this page helpful?