title: How To Project a Line Number Into Linq Query Results
body: <p>How can I project the row number onto the linq query result set.</p>
  
  <p>Instead of say:</p>
  
  <p>field1, field2, field3</p>
  
  <p>field1, field2, field3</p>
  
  <p>I would like:</p>
  
  <p>1, field1, field2, field3</p>
  
  <p>2, field1, field2, field3</p>
  
  <p>Here is my attempt at this:</p>
  
  <pre><code>public List&lt;ScoreWithRank&gt; 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&lt;ScoreWithRank&gt;();
      }
  }
  </code></pre>
  
  <p>Unfortunately, the "Rank=i++" line throws the following compile-time exception:</p>
  
  <p>"An expression tree may not contain an assignment operator"</p>