I have list of string as this:
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)}"
What I have is:
this is name of top score students
jim
john
mary
What I need is:
this is name of top score students
1-jim
2-john
3-mary
The problem is that the number of topScores is dynamic, how can i achive the above list?
Change your select to:
.Select((s, i) => (i+1) + "-" + s.name)
This overload of the Select
method will pass in the index as the second parameter to the lambda expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With