Could I suggest that you do something like this? Group your Reviews
colletion by RestaurantId
as the key and use the Mark
property for the collection that relates to that key. Output the result for each group as a value tuple consisting of the RestaurantId
and the average value of the Mark
collection. The Average
method takes an enumerable of ints
and returns a double
so there is no need to convert the Mark
property into a double
.
var reviewResults = Reviews.GroupBy(r => r.RestaurantId, r => r.Mark, (key, marks) => (Id:key,AverageMark: marks.Average()));
Then join the reviewResults
to the Restaurants
collection by RestaurantId
to include any addional properties from the Restaurants
collection that you require.
var summaries= reviewResults.Join(Restaurants, rev => rev.Id, rest => rest.RestaurantId, (rev, rest) => (rest.Name,rev.AverageMark, rev.Id));
You can enumerate the summaries
like this
foreach(var(Name,Mark,Id)in summaries)
{
Console.WriteLine($"Restaurant {Name} Average Score {Mark}");
}
There is no need for the summaries
variable. I just put that in to simplify the explanation, you can join the two Linq statements together using the dot notation.