1

I have a list of objects I want to group. Objects have a List parameter, and during grouping I want to make the sum of the lists like this :

for(int i=0;i<MyList1.Count();i++)
{
StatutOperations[i]=StatutOperations1[i]+StatutOperations2[i]...
}

For now using linq I have the following :

liste_rep = liste_rep.GroupBy(l => l.Nom)
                    .Select(cl => new Repere
                    {
                        Quantite = cl.Sum(c => c.Quantite),
                        IdAff = cl.First().IdAff,
                        ID = 0,
                        ListeOperations = cl.First().ListeOperations,
                        StatutOperations = cl.Zip(StatutOperations)//First().StatutOperations

                    }).ToList();

The line making problem is the last one, I found how to use Zip function to summ two tables, but what if I want to use it grouping Lists?

Edit : StatusOperations is a list of integers, concretely liste_rep is a list of details, details have a list of n operations, and StatusOperations determines how much details have been operated for each operation.

Example : ListOperations = CUT, DRILL, PAINT StatusOperations = 20,20,10

This means 20 details are cut, 20 are drilled and 10 are painted I want to group the list of details getting totals for each operation.

Edit 2 :

For now I only could manage to do it making myself the grouping :

                liste_rep = liste_rep.OrderBy(p => p.Nom).ToList();
                if (liste_rep.Count()>1)
                {
                    totalStatut = liste_rep[0].StatutOperations.ConvertAll(s => s = 0);
                    string oldRep = "";
                    Repere repere = new Repere();
                    foreach (Repere rep in liste_rep)
                    {
                        if (rep.Nom!=oldRep)
                        {
                            newListRep.Add(repere);
                            repere = new Repere();
                            repere.Nom = rep.Nom;
                            repere.StatutOperations = rep.StatutOperations;
                        }
                        else
                        {
                            repere.StatutOperations=repere.StatutOperations.Zip(rep.StatutOperations, (x, y) => x + y).ToList();
                        }
                        oldRep = rep.Nom;
                    }
                }
Siegfried.V
  • 1,508
  • 1
  • 16
  • 34

1 Answers1

2

You can use this

if StatutOperations is a list of int).

Use this at last line. StatutOperations= cl.Aggregate((opl1, opl2) =>
{ return opl1.StatutOperations.Zip(opl2.StatutOperations, (opin1,opin2)=>opin1+opin2).ToList(); });

in above code Aggregate runs through two elements and aggregate as sum (op1+op2).

Note : Remember use aggregate if and only if list contains more than one element . Edit: Sorry the above code is incorrect as this is applying aggregate on repere type object and hence the expected return value would be of Repere type.

Edited my code now it should work fine now.

liste_rep.GroupBy(l => l.Nom)
                    .Select(cl => new Repere
                    {
                        Quantite = cl.Sum(c => c.Quantite),
                        IdAff = cl.First().IdAff,
                        ID = 0,
                        ListeOperations = cl.First().ListeOperations,
                        StatutOperations = cl
                                           .Select(x=>x.StatutOperations)
                                           .Aggregate((x,y)=> x.Zip(y,(p,q)=>p+q).ToList());
                    }).ToList();
  • Hi, thanks for answer, but not working , I have two errors 1) Cannot convert List to Repere(it is my object), 2) cannot convert lamda expression. for information, my list can have 1 to hundreds elements(usualy several, but can happen that there is only one element). – Siegfried.V Dec 31 '17 at 06:31
  • Hi for one element you can directly assign it like with 0 index – Neeraj Bhushan Dec 31 '17 at 11:24
  • Edit2 is working yes, but I wanted to try it using qlinq , this is my problem, for now I use my method, but I know this is not the best way(then if only one element I just don't do grouping, I put a condition) – Siegfried.V Jan 01 '18 at 12:59
  • Hi edited my answer it should work fine with the edited code. Sorry for previous answer it was wrong – Neeraj Bhushan Jan 01 '18 at 17:09
  • Hi, thanks a lot, your new code works like a charm :) Thank you and happy new year :) – Siegfried.V Jan 02 '18 at 06:49
  • Your Welcome. Happy new Year :) – Neeraj Bhushan Jan 02 '18 at 09:22