2008-12-13 62 views
34

如何將行號投影到linq查詢結果集上。如何將行號投影到Linq查詢結果

你可以這樣說的:

字段1,字段2,字段3

字段1,字段2,字段3

我想:

1,字段1,字段2,字段3

2 ,field1,field2,field3

這是我的嘗試在此:

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

不幸的是,「等級= I ++」線引發以下的編譯時例外:

「表達式樹可以不包含一個賦值操作符」

+0

[你如何索引字段添加到LINQ結果的可能的複製(http://stackoverflow.com/questions/269058/how-do-you-add-an-index-field- to-linq-results) – 2016-07-13 03:28:25

回答

55

好,最簡單的方法是將它做在客戶端,而不是數據庫端,並使用選擇的過載它提供了一個指標,以及:

public List<ScoreWithRank> GetHighScoresWithRank(string gameId, int count) 
{ 
    Guid guid = new Guid(gameId); 
    using (PPGEntities entities = new PPGEntities()) 
    { 
     var query = from s in entities.Scores 
        where s.Game.Id == guid 
        orderby s.PlayerScore descending 
        select new 
        { 
         PlayerName = s.PlayerName, 
         PlayerScore = s.PlayerScore 
        }; 

     return query.AsEnumerable() // Client-side from here on 
        .Select((player, index) => new ScoreWithRank() 
          { 
           PlayerName = player.PlayerName, 
           PlayerScore = player.PlayerScore, 
           Rank = index + 1; 
          }) 
        .ToList(); 

    } 
} 
+3

從數據庫中獲取一切並不是一個真正的'解決方案' – Adaptabi 2011-04-27 08:42:33

+1

@DNetNet:它沒有從數據庫中獲得*所有內容 - 只有匹配查詢的位。它只能從原始數據庫中獲取相同數量的數據 - 只需進行一些後期處理即可。 – 2011-04-27 08:52:07

+0

如何? query.AsEnumerable()將爲給定的gameId提供所有匹配的記錄。只嘗試在20日以後排名的職位。 你會得到一切從數據庫爲了有秩序,然後削減你所需要的。 不是真正想要的解決方案! 除此之外 - 其中是使用的計數參數? – Adaptabi 2011-06-25 08:49:46

1

好吧,這確實起作用。謝謝。

這是我最後的代碼...

服務器:

public List<Score> GetHighScores(string gameId, int count) 
{ 
    Guid guid = new Guid(gameId); 
    using (PPGEntities entities = new PPGEntities()) 
    { 
     var query = from s in entities.Scores 
        where s.Game.Id == guid 
        orderby s.PlayerScore descending 
        select s; 
     return query.ToList<Score>(); 
    }                  
} 

客戶:

void hsc_LoadHighScoreCompleted(object sender, GetHighScoreCompletedEventArgs e) 
{ 
    ObservableCollection<Score> list = e.Result; 

    _listBox.ItemsSource = list.Select((player, index) => new ScoreWithRank() 
          { 
           PlayerName = player.PlayerName, 
           PlayerScore = player.PlayerScore, 
           Rank = index+=1 
          }).ToList(); 
} 
0

你也可以將只是一個小幅調整,以您的原代碼,使其能工作。謹慎的話,如果你再次綁定或訪問對象,秩會每次遞增。在這些情況下,最好的答案是更好的。

let Rank = i++ 

Rank.ToString() 

全碼:

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 
       let Rank = i++ 
       where s.Game.Id == guid 
       orderby s.PlayerScore descending 
       select new ScoreWithRank() 
       { 
        Rank.ToString(), 
        PlayerName = s.PlayerName, 
        PlayerScore = s.PlayerScore 
       }; 
    return query.ToList<ScoreWithRank>(); 
} 

}