public IEnumerable <UserWithScore> GetTotalScores() { using (var ctx = new HighscoreContext()) { return(ctx.Scores.GroupBy(u => u.User).Select(g => new UserWithScore() { ID = g.Key.ID, Name = g.Key.Name, Score = g.Sum(s => s.Points) }) .OrderByDescending(o => o.Score) .Take(10) .ToList()); } }
public int CreateGame(string name) { using (var ctx = new HighscoreContext()) { var game = new Game() { Name = name }; var x = ctx.Games.Add(game); ctx.SaveChanges(); return(x.ID); } }
public void CreateScore(int userId, int gameId, int points) { using (var ctx = new HighscoreContext()) { User user = null; foreach (var u in ctx.Users.Where(s => s.ID == userId)) { user = u; } Game game = null; foreach (var g in ctx.Games.Where(s => s.ID == gameId)) { game = g; } var score = new Score() { User = user, Game = game, Points = points }; var x = ctx.Scores.Add(score); ctx.SaveChanges(); } }
static void Main(string[] args) { Controller c = new Controller(); using (var ctx = new HighscoreContext()) { c.CreateScore(c.CreateUser("Herbert"), c.CreateGame("Fortnite"), 1500); c.CreateScore(c.CreateUser("Markus"), c.CreateGame("Fortnite"), 4531); c.CreateScore(c.CreateUser("Martin"), c.CreateGame("WoW"), 3200); c.CreateScore(c.CreateUser("Mark"), c.CreateGame("Tetris"), 32158); c.CreateScore(c.CreateUser("Robert"), c.CreateGame("WoW"), 888654); c.CreateScore(c.CreateUser("Lutz"), c.CreateGame("WoW"), 4605); foreach (var sc in ctx.Scores.Include("Game").Include("User").Where(s => s.Points > 4600)) { Console.WriteLine("{1} in Spiel {2}: {0}", sc.Points, sc.User.Name, sc.Game.Name); } } }