gpt4 book ai didi

c# - linq查询中的自动编号

转载 作者:太空狗 更新时间:2023-10-30 00:14:07 25 4
gpt4 key购买 nike

我有这样的字符串列表:

 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 的数量是动态的,我怎样才能获得上面的列表?

最佳答案

将您的选择更改为:

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

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

关于c# - linq查询中的自动编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com