Làm thế nào tôi có thể chiếu số hàng vào tập kết quả truy vấn LINQ.Làm thế nào để chiếu một số dòng vào kết quả truy vấn LINQ
Thay vì nói:
field1, field2, field3
field1, field2, field3
Tôi muốn:
1, field1, field2, field3
2 , field1, field2, field3
Đây là nỗ lực của tôi tại đây:
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>();
}
}
Thật không may, "Rank = i ++" dòng ném thời gian biên dịch ngoại lệ sau đây:
"Một cây biểu hiện có thể không chứa một toán tử gán"
có thể trùng lặp của [Làm thế nào để bạn thêm một lĩnh vực chỉ số để kết quả LINQ] (http://stackoverflow.com/questions/269058/how-do-you-add-an-index-field- to-linq-results) –