I have two methods which are very close, and want to create one method that I can call to keep my code DRY
public IEnumerable<HighScoreViewModel> GetNightlyHighScores()
{
lock (_Key)
{
//return GetHighScores(x => x.NightlyHighScore);
return _allPlayers
.OrderByDescending(x => x.NightlyHighScore)
.Take(8)
.Select(x=> new HighScoreViewModel
{
Points = x.NightlyHighScore,
PointsText = "FG: " + x.NightlyHighScore,
ImageUrl = x.Facebook.SmallImageUrl,
DisplayName = x.DisplayName,
}).ToList();
}
}
public IEnumerable<HighScoreViewModel> GetBestHighScores()
{
lock (_Key)
{
//return GetHighScores(x => x.BestHighScore);
return _allPlayers
.OrderByDescending(x => x.BestHighScore)
.Take(8)
.Select(x => new HighScoreViewModel
{
Points = x.BestHighScore,
PointsText = "FG: " + x.BestHighScore,
ImageUrl = x.Facebook.SmallImageUrl,
DisplayName = x.DisplayName,
}).ToList();
}
}
I’m close to something, but can’t figure out the bits marked “magic”
public IEnumerable<HighScoreViewModel> GetHighScores<TKey>(Func<Player, TKey> highscore)
{
return _allPlayers
.OrderByDescending(highscore)
.Take(8)
.Select(x => new HighScoreViewModel
{
// Magic?
//Points = x.BestHighScore,
PointsText = "FG: " + x.BestHighScore,
//ImageUrl = x.Facebook.SmallImageUrl,
DisplayName = x.DisplayName,
}).ToList();
}
Since
BestHighScoreandNightlyHighScorehave the same type, you should be able to do:highscoreshould return whatever the real type ofPointsis.