我有兩個方法,這是非常密切的,並希望創建一個方法,我可以打電話讓我的代碼幹創建具有LINQ方法
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();
}
完美,謝謝!我會在兩分鐘內完成它標記 –