使用Linq查询+可选参数避免重复代码

时间:2013-07-01 13:13:12

标签: c# linq refactoring dry

鉴于代码:

/// <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;
} 

skiptake是可选参数(用于需要分页时),获取正确记录的最佳方法是什么,而不像上面那样重复自己?

4 个答案:

答案 0 :(得分:9)

不是在四个单独的查询之间切换,而是根据您的条件构建单个查询:

IQueryable<Score> query = db.ArcadeScores.Where(c => c.GameID == gameID);

switch(type)
{
    case HighScoreType.ScoreRank:
        query = query.Where(c => c.ScoreRank > 0).OrderBy(c => c.ScoreRank);
        break;
    case HighScoreType.UserRank:
        query = query.Where(c => c.UserRank > 0).OrderBy(c => c.UserRank);
        break;
}

if (skip.HasValue && take.HasValue)
   query = query.Skip(skip.Value).Take(take.Value);

return query.ToList();

答案 1 :(得分:1)

鉴于在您致电IEnumerable之前未执行ToList,您只需将其移至switch语句之外,然后致电ToList

答案 2 :(得分:1)

只需使用coalesce运算符即可提供默认值:

.Skip(skip ?? 0)
.Take(take ?? int.MaxValue)

答案 3 :(得分:0)

或许,将其解压缩为一个方法,以便只在需要分页时调用OrderBy和Skip。

db.ArcadeScores.Where(c => c.GameID == gameID && c.ScoreRank > 0)
                                .OrderBy(c => c.ScoreRank).

db.ArcadeScores.Where(c => c.GameID == gameID && c.UserRank > 0)
                            .OrderBy(c => c.UserRank)

private IEnumerable<ArcadeScore> GetArcadeOverallScore(int gameId)
{
   return db.ArcadeScores.Where(c => c.GameID == gameID && c.ScoreRank > 0)
                                    .OrderBy(c => c.ScoreRank)
}

private IEnumerable<ArcadeScore> GetArcadeUserScore(int gameId)
{
   return db.ArcadeScores.Where(c => c.GameID == gameID && c.UserRank > 0)
                                .OrderBy(c => c.UserRank)
}