linq查询中的自动编号

时间:2015-10-13 14:29:37

标签: c# linq list

我有这样的字符串列表:

 var TopScores=
                list.Where(s => s.Score>2500)
                    .OrderBy(s => s.Score)
                    .Select(s => s.name)
                    .ToList();

var text=  $"{"this is name of top score students"}\n{string.Join("\n", topScores)}"

我拥有的是:

this is name of top score students
jim
john
mary

我需要的是:

this is name of top score students
1-jim
2-john
3-mary

问题是topScores的数量是动态的,我怎么能得到上面的列表呢?

1 个答案:

答案 0 :(得分:11)

将您的选择更改为:

.Select((s, i) => (i+1) + "-" + s.name)

Select方法的这个重载将作为lambda表达式的第二个参数传入索引。

相关问题