use entity framework to get the highest value from a column named "high_score", then congratulate the user on their accomplishments in csharp

main.cs
using(var db = new MyContext())
{
  var highestScore = db.Players.Max(p => p.HighScore);
  var playerName = db.Players.Where(p => p.HighScore == highestScore).Select(p => p.Name).First();

  Console.WriteLine($"Congratulations {playerName} for achieving the highest score of {highestScore}!");
}
296 chars
8 lines

Explanation:

  • We start by creating a new context object, MyContext, which should inherit from DbContext.

  • We then use the Max method to retrieve the highest score from the HighScore column of the Players table.

  • Next, we use the Where method to retrieve the player(s) with the highest score. We only expect one player, so we use the First method to select the first item in the result set.

  • Finally, we use Console.WriteLine to print out a congratulatory message with the name of the player and their high score.

gistlibby LogSnag