Automatic numbering in linq request

I have a list of lines:

 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)}"

I have:

this is name of top score students
jim
john
mary

What I need:

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 get the above list?

+4
source share
1 answer

Change your selection to:

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

This method overload Selectwill take place in the index as the second parameter in the lambda expression.

+11
source

Source: https://habr.com/ru/post/1611448/


All Articles