我有这样的代码:
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))
)));
}
我想知道你是否能以某种方式计算每支球队的胜利,而不是先获得球队名称之后的球队名称。任何想法/
答案 0 :(得分:5)
您应该可以将foreach
替换为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();
答案 1 :(得分:0)
您也可以直接在匹配项上使用groupby,而无需创建teamNames。