给定代码:
/// <summary>
/// Get a games high scores
/// </summary>
public static List<Score> GetGameHighScores(int gameID, HighScoreType type, int? skip = null, int? take = null)
{
var r = new List<Score>();
using (var db = new ArcadeContext())
{
var q = new List<ArcadeScore>();
if (skip != null && take != null)
{
switch (type)
{
case HighScoreType.ScoreRank:
q =
db.ArcadeScores.Where(c => c.GameID == gameID && c.ScoreRank > 0)
.OrderBy(c => c.ScoreRank)
.Skip(skip.Value)
.Take(take.Value)
.ToList();
break;
case HighScoreType.UserRank:
q =
db.ArcadeScores.Where(c => c.GameID == gameID && c.UserRank > 0)
.OrderBy(c => c.UserRank)
.Skip(skip.Value)
.Take(take.Value)
.ToList();
break;
}
}
else
{
switch (type)
{
case HighScoreType.ScoreRank:
q =
db.ArcadeScores.Where(c => c.GameID == gameID && c.ScoreRank > 0)
.OrderBy(c => c.ScoreRank)
.ToList();
break;
case HighScoreType.UserRank:
q =
db.ArcadeScores.Where(c => c.GameID == gameID && c.UserRank > 0)
.OrderBy(c => c.UserRank)
.ToList();
break;
}
}
r.AddRange(q.Select(arcadeScore => new Score(arcadeScore)));
}
return r;
}
可选参数在哪里skip
和take
是可选参数(用于需要分页时),获取正确记录而不像上面那样重复自己的最佳方法是什么?