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

Answers


Close. Try this:

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

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)});

Need Your Help

UIImagePickerController - Setting a maximum video duration

iphone objective-c ios4 ios

I am using the following code to allow users to record video using their iPhone camera, and then upload it directly to my server. I would like to put a limit on the length of the video they can rec...

Simulating online user in eJabberd with Tsung

erlang xmpp ejabberd tsung

I'm testing eJabberd server with Tsung on local computer. This is my config file. Everything is working and running correctly.