LINQ, iterators, selection and projection

What I would like to do is use LINQ elegance while maintaining the iterator ....

substantially

Class A { int Position; string Name; } 

If I have a list of strings, I want to project them into List<A> , but the position will be entered in the position ...

 List<string> names; //filled with strings 

sort of

 List<A> foo = (from s in names select s).ToList(); 

but he also goes through and fills the position.

Is it possible?

 {{Position:0,Name: "name1"},{Position:1, Name: "name2"}, {Position:2, Name: "name3"}....} 
+4
source share
1 answer

You can do it:

  var listOfStrings = new List<string> {"name1", "name2", "name3", "name4"}; var foo = listOfStrings.Select((value, position) => new {position, value}).ToList(); 

The position will be increased as a 0-start index, check the Method Hook .

+10
source

All Articles