【发布时间】:2010-09-26 18:39:06
【问题描述】:
如何将行号投影到 linq 查询结果集。
而不是说:
字段1,字段2,字段3
字段1,字段2,字段3
我想要:
1,字段1,字段2,字段3
2,字段1,字段2,字段3
这是我的尝试:
public List<ScoreWithRank> GetHighScoresWithRank(string gameId, int count)
{
Guid guid = new Guid(gameId);
using (PPGEntities entities = new PPGEntities())
{
int i = 1;
var query = from s in entities.Scores
where s.Game.Id == guid
orderby s.PlayerScore descending
select new ScoreWithRank()
{
Rank=i++,
PlayerName = s.PlayerName,
PlayerScore = s.PlayerScore
};
return query.ToList<ScoreWithRank>();
}
}
很遗憾,“Rank=i++”行会引发以下编译时异常:
“表达式树不能包含赋值运算符”
【问题讨论】:
标签: linq row-number