C
C#2y 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
};
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);
});
});
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);
});
});
2 Replies
dino2dy
dino2dyOP2y ago
My latest attempt
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
} into gcse
select new
{
srecka= gcs.Key.sreSif,
serija = gcs.Key.sreSerija,
opBroj = gcse.Key.sreIsplatio,
sreckeTemp = gcse.ToList(),
};

foreach (var c in consolidatedChildren)
{
foreach (var isp in c.sreckeTemp)
{

List<string>IsplatniBrojevi= new List<string>();
IsplatniBrojevi.Add(isp.sreIspBroj);
}
}
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
} into gcse
select new
{
srecka= gcs.Key.sreSif,
serija = gcs.Key.sreSerija,
opBroj = gcse.Key.sreIsplatio,
sreckeTemp = gcse.ToList(),
};

foreach (var c in consolidatedChildren)
{
foreach (var isp in c.sreckeTemp)
{

List<string>IsplatniBrojevi= new List<string>();
IsplatniBrojevi.Add(isp.sreIspBroj);
}
}
But I don't see gcs so those lines are showing me errors
select new
{
srecka= gcs.Key.sreSif,
serija = gcs.Key.sreSerija,
select new
{
srecka= gcs.Key.sreSif,
serija = gcs.Key.sreSerija,
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server