我有两种非常接近的方法,并且想要创建一种可以调用以保持代码干燥的方法
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();
}
}
我接近某事,但无法弄清楚标记为“魔法”的位
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();
}