LINQ: Data + sequential number into new list

I have a list of the type SomethingDTO and I now want to generate a new list of the type SomethingNeuDTO with 2 Values: “Bezeichnung” and “Nummer”, where Nummer is simply a sequential number from 1 to x. To make this work in one step I can do this:

List<SomethingDTO> original; // the original list
var neueListe = original.AsEnumerable()
.Select((a, index) => new SomethingNeuDTO {
    Bezeichnung = a.Bezeichnung,
    Nummer = index + 1
}).ToList();

The trick is AsEnumerable, which makes it possible to access the index.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.