I've got a table of transaction data for several locations and I need to find the sum of each location's maximum from a subset of locations. So imagine the following table:
location year transactions
123 2009 57
124 2009 23
125 2009 45
123 2010 64
124 2010 12
125 2010 66
So if I'm looking just for the data for locations 123 and 124 the code should pick out the value 64 for location 123 in 2010 and 23 for location 124 in 2009.
I've got the following code which works in that it finds the maximum value for each location and then adds it to the running total.
int total = 0;
foreach (var location in locationIds)
{
int? temp = transactions.Where(t => t.Location == location)
.Max(t => t.Transactions);
if (temp.HasValue)
{
total += temp.Value;
}
}
Is there a more elegant way of coding this?