1

次のモデルを使用して、1 人のプレーヤーが獲得したポイント数と、特定の期間にすべてのプレーヤーが獲得した平均ポイント数を取得できるようにしたいと考えています。これは、単一のデータベースクエリで実行する必要があります(後で来るチームごとの平均ポイントなど、他の統計も必要ですが、概念は同じである必要があります)。

悪い一日を過ごし、どこにも行きません。誰かが私を助けることができますか?

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(),                            
                        };

したがって、コメントで述べたように、その期間にプレーしていないプレーヤーを考慮に入れるために AveragePoints の計算が必要です。

4

1 に答える 1

0

GameクラスにはDateTimeプロパティがあると想定しました。基本的な考え方は、group by 1トリックを使用することです

DateTime startDateTime, endDateTime;

int playerId;

var query = from p in context.Players
            join pg in context.Players on p.Id equals pg.PlayerId
            join g in context.Games on pg.GameId equals g.Id               
            group new { p, pg, g } by 1 into ppgg
            select new {
               SinglePlayerPointsGained = (from x in ppgg
                                           where x.p.PlayerId == playerId
                                           where x.g.DateTime >= startDateTime && x.g.DateTime <= endDateTime
                                           select x.pg.Points ).Sum(),
               AveragePoints = (from x in ppgg
                                group x.pg.Points by x.p.PlayerId into g
                                select g.Key).Average()

            };
于 2012-11-06T20:24:49.233 に答える