如何使用LINQ计算每场比赛中一组中每个团队的进球差
这是我到目前为止所得到的,这只是给定目标的总和,没有实现。
GoalDifference =
context.Games
.Where(u => u.HomeTeam == t.TeamId)
.Sum(t1 => t1.HomeGoals) +
context.Games
.Where(u => u.AwayTeam == t.TeamId)
.Sum(t1 => t1.AwayGoals);
答案 0 :(得分:0)
您需要有两个不同的ID,一个是主队,另一个是客队。然后
GoalDifference =
context.Games
.Where(u => u.HomeTeam == homeTeamId && u.AwayTeam == awayTeamId )
.Sum(t1 => Math.Abs(t1.HomeGoals - t1.AwayGoals))
或者您都需要同时拥有MatchId,然后
GoalDifference =
context.Games
.Where(u => u.MatchId == matchId )
.Sum(t1 => Math.Abs(t1.HomeGoals - t1.AwayGoals))
答案 1 :(得分:0)
我想说goal difference
是您想要的:
GoalDifference =
//Home goal difference
context.Games
.Where(u => u.HomeTeam == t.TeamId)
.Sum(g1 => g1.HomeGoals - g1.AwayGoals) +
//Away goal difference
context.Games
.Where(u => u.AwayTeam == t.TeamId)
.Sum(g1 => g1.AwayGoals- g1.HomeGoals);
请注意,您选择的不是sum
内的球队,所以g1
比t1
更好的名字