给定以下模型,我希望能够检索单个玩家获得的积分数以及给定时间段内所有玩家获得的平均积分数。这应该在单个数据库查询中完成(我还想要其他统计数据,例如每个团队的平均得分,稍后会出现,但概念应该是相同的)。
度过了糟糕的一天,却一无所获。有人可以帮我吗?
public class Player
{
public int Id { get; set; }
public ICollection<PlayerGame> PlayerGames { get; set; }
...
}
public class PlayerGame
{
public int Id { get; set; }
public int Points { get; set; }
public int PlayerId { get; set; }
public Player Player { get; set; }
public int GameId { get; set; }
public Game Game { get; set; }
...
}
public class Game
{
public int Id { get; set; }
...
}
编辑:
好的。暂时将游戏实体排除在外,并更改您的代码以适应我的回购。这就是我现在所拥有的:
var query = from p in _playerRepository.Query()
from pg in p.PlayerGames
group new { p, pg } by 1 into ppg
select new
{
SinglePlayerPointsGained = (from x in ppg
where x.p.Id == playerId && x.pg.Date > startDateTime
select x.pg.Points).Sum(),
AveragePoints = (from x in ppg
where x.pg.Date > startDateTime
select x.pg.Points).Average(),
};
所以现在我只需要平均点数计算来考虑在评论中提到的那个时期没有参加过比赛的球员。