2010-10-06 154 views
1

,如果我有這樣的結構:分組拉姆達

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 

而且我想要得到的東西像

Batch Amount 
76 1938.61 
77 5106.58 

如何表達的應該是什麼?

我開始喜歡的東西:

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

但它並不完全是我要找的東西。幫助我解決問題。謝謝

回答

5

關閉。試試這個:

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

噢謝謝你... – Agzam 2010-10-06 16:07:29

1

查詢將看起來像:

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

雖然我不知道什麼是做的拉姆達相當於將

UPD:

ReSharper的幫助很大! :)

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