2010-10-06 6 views
1

si j'ai une structure comme ceci:Groupement lambda

Batch Amount 
76 495.4 
76 975.75 
76 25 
76 442.46 
77 1335.12 
77 2272.37 
77 34.5 
77 496.99 
77 360 
77 13 
77 594.6 

Et je veux obtenir quelque chose comme

Batch Amount 
76 1938.61 
77 5106.58 

Comment l'expression devrait être?

J'ai commencé avec quelque chose comme:

batches.GroupBy(x => new { Batch = x.Batch, Amount = x.Amount }); 

Mais il est pas tout à fait la chose que je cherche. Aide-moi à bien faire les choses. Merci

Répondre

5

Fermez. Essayez ceci:

batches.GroupBy(x => x.Batch, x => x.Amount).Select(g => new { 
    Batch = g.Key, 
    Amount = g.Sum() 
}); 
+0

Oh merci ... – Agzam

1

La requête sera ressembler à:

var bs = from s in batches 
      group s by s.Batch into g 
      select new { Batch = g.Key, Amount = g.Sum(p => p.Amount) }; 

Bien que je ne sais pas ce qui est l'équivalent lambda serait

màj:

ReSharper aide beaucoup ! :)

var batches = bs .GroupBy(s => s.Batch).Select(
       g => new {Batch = g.Key, Amount = g.Sum(p => p.Amount)});