I have an object <Repere>
whose structure is the following :
class Repere
{
public string Name { get; set; }
public List<Operation> Operations { get; set; }
public int Quantite {get;set;}
...
}
When I want to group a List<Repere>
by its name, I always use the following grouping method :
List<Repere> liste_rep_group = liste_rep.GroupBy(l => l.Name)
.Select(cl => new Repere
{
Quantite = cl.Sum(c => c.TotalQuantity),
TotalQuantity = cl.Sum(c => c.TotalQuantity),
ID = -1,
IdAff = cl.First().IdAff,
Name = cl.First().Name,
NameOri = cl.First().Name,
Nom_aff = cl.First().Nom_aff,
Profil = cl.First().Profil,
Longueur = cl.First().Longueur,
Hauteur = cl.First().Hauteur,
Largeur = cl.First().Largeur,
Poids = cl.First().Poids,
Priorite = cl.Min(c => c.Priorite),
Matiere = cl.First().Matiere,
Angle1 = cl.First().Angle1,
Angle2 = cl.First().Angle2,
AngleAile1 = cl.First().AngleAile1,
AngleAile2 = cl.First().AngleAile2,
GroupeProfil = cl.First().GroupeProfil,
ListOperations = (cl.SelectMany(g=>g.ListOperations).GroupBy(o=>o.ID)
.Select(go => new Operation
{
ID = go.First().ID,
QtyFinished = go.Sum(o => o.QtyFinished),
Color=go.First().Color,
})).ToList()
...
}).ToList();
What I would liek to do is add in my class Repere a method like
public List<Repere> GroupByName(List<Repere>)
{
//My grouping method
}
So everytime I need to group my List<Repere>
, I don't need to copy all the function everywhere.The main reason is that when I modify Repere structure, I need to edit function, and there are risks to forget to update it somewhere.