tags:

views:

29

answers:

2
+1  Q: 

Grouping lambda.

if I have a structure like this:

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

And I want to get something like

Batch  Amount
76    1938.61
77    5106.58

How the expression should be?

I started with something like:

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

But it's not quite the thing that I'm looking for. Help me to get it right. Thanks

+4  A: 

Close. Try this:

batches.GroupBy(x => x.Batch, x => x.Amount).Select(g => new {
    Batch = g.Key,
    Amount = g.Sum()
});
mquander
Oh thank you...
Ike
+1  A: 

The query will be look like:

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

Although I don't know what's the lambda equivalent would be

upd:

Resharper helps a lot! :)

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