score:9

Accepted answer

instead of switching between four separate queries, build single query according to your conditions:

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();

score:0

probably, extract this to a method so that you would only call orderby and skip when they want the paginated one.

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)

into

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)
}

score:1

given that the ienumerable is not executed until you call tolist you can simply move those outside the switch statement and then call tolist

score:1

simply use the coalesce operator to provide default values:

.skip(skip ?? 0)
.take(take ?? int.maxvalue)

Related Query

More Query from same tag