How To Project a Line Number Into Linq Query Results

前端 未结 5 1483
栀梦
栀梦 2020-11-27 14:04

How can I project the row number onto the linq query result set.

Instead of say:

field1, field2, field3

field1, field2, field3

I would like:<

5条回答
  •  一生所求
    2020-11-27 15:05

    Ok, that did the trick. Thanks.

    Here is my final code...

    Server:

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

    Client:

    void hsc_LoadHighScoreCompleted(object sender, GetHighScoreCompletedEventArgs e)
    {
        ObservableCollection list = e.Result;
    
        _listBox.ItemsSource = list.Select((player, index) => new ScoreWithRank()
                                {
                                    PlayerName = player.PlayerName,
                                    PlayerScore = player.PlayerScore,
                                    Rank = index+=1
                                }).ToList();
    }
    

提交回复
热议问题