was hoping for a little help.
I am retrieving the following list from a database.
Name AssessNumber Score
John 1 90
John 2 88
John 3 67
Steve 1 98
Steve 2 90
what I want is to group by average scores and display highest asessment number(int)
Name AssessNumber Score
John 3 81.6
Steve 2 94
The data will be stored in a List<lbResults>lb = new List<lbResults>()
How can I achieve this?
kind Regards
Try this:
var results = data.GroupBy(x => x.Name).Select(x => new
{
Name = x.Key,
AssessNumber = x.Max(z => z.AssessNumber),
Score = x.Average(z => z.Score)
})
You can also return the original data structure (which you named lbResults):
var results = data.GroupBy(x => x.Name).Select(x => new lbResults
{
Name = x.Key,
AssessNumber = x.Max(z => z.AssessNumber),
Score = x.Average(z => z.Score)
})
See Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With