Reputation: 886
I've got a code like this:
string[] teamNames = teams.Select(x => x.Name).ToArray();
List<int> wins = new List<int>();
foreach(var _team in teamNames)
{
wins.Add(matches.Count(x =>
((x.Team1 == _team && x.Maps.Count(map => map.Score1 > map.Score2) > x.Maps.Count(map => map.Score2 > map.Score1)) ||
(x.Team2 == _team && x.Maps.Count(map => map.Score2 > map.Score1) > x.Maps.Count(map => map.Score1 > map.Score2))
)));
}
I was wondering if you could somehow count the wins for each team without the foreach first which just yields team name after team name. Any ideas for that/
Upvotes: 2
Views: 396
Reputation: 152
You can also use groupby directly on the matches without creating the teamNames.
Upvotes: 0
Reputation: 726579
You should be able to do it by replacing the foreach
with a Select
:
var wins = teamNames
.Select(_team =>
matches.Count(x =>
((x.Team1 == _team && x.Maps.Count(map => map.Score1 > map.Score2) > x.Maps.Count(map => map.Score2 > map.Score1)) ||
(x.Team2 == _team && x.Maps.Count(map => map.Score2 > map.Score1) > x.Maps.Count(map => map.Score1 > map.Score2))
))
)
.ToArray();
Upvotes: 5