What I would like to do is use the elegance of LINQ while maintaining an iterator....
essentially
Class A
{
int Position;
string Name;
}
if I have a list of strings, i want to project them into List<A>
but have the Position be populated in the projection...
List<string> names; //filled with strings
something like
List<A> foo = (from s in names select s).ToList();
but have it also iterate over and populate Position..
is this possible?
{{Position:0,Name: "name1"},{Position:1, Name: "name2"}, {Position:2, Name: "name3"}....}
You can do this:
var listOfStrings = new List<string> {"name1", "name2", "name3", "name4"};
var foo = listOfStrings.Select((value, position) => new {position, value}).ToList();
Position will be incremented as a 0-starting index, check the Select Method overload.
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