How To Project a Line Number Into Linq Query Results

前端 未结 5 1466
栀梦
栀梦 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 14:44
    List<Emp> Lstemp = GetEmpList(); 
    int Srno = 0; 
    var columns = from t in Lstemp 
                  orderby t.Name 
                  select new { 
                      Row_number=++Srno, 
                      EmpID = t.ID, 
                      Name = t.Name, 
                      City = t.City 
                  };
    
    0 讨论(0)
  • 2020-11-27 14:47

    You could also make just a slight adjustment to your original code to get it working. Word of caution, if you databind or access the object again, the Rank will increment each time. In those cases the top answer is better.

    let Rank = i++
    

    and

    Rank.ToString()
    

    Full code:

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

    }

    0 讨论(0)
  • 2020-11-27 14:53

    Well, the easiest way would be to do it at the client side rather than the database side, and use the overload of Select which provides an index as well:

    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();
    
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:03

    This solution worked for me. http://www.dotnetfunda.com/articles/article1995-rownumber-simulation-in-linq.aspx

    .Select((x, index) => new
    {
         SequentialNumber = index + 1
        ,FieldFoo = x.FieldFoo                        
    }).ToList();
    
    0 讨论(0)
  • 2020-11-27 15:05

    Ok, that did the trick. Thanks.

    Here is my final code...

    Server:

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

    Client:

    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 讨论(0)
提交回复
热议问题