Juan Carlos Oropeza Juan Carlos Oropeza - 3 months ago 8
C# Question

How create one autonumeric properties from a linq select

I want create a

List<myItem>
from a single string to fill the
myValue
property.

string:

"233 984 5 66 304 219 277 790"


class:

public class myItem
{
public long ID; // autonumeric id go here
public long myValue; // string values go here
public string otherProperties; //I will fill later from other source
}


I can read the string, split using space and parse to
long[]
:

string line1 = "233 984 5 66 304 219 277 790";
string[] words = line1.Split(' ');
long[] myValues = Array.ConvertAll(words, s => long.Parse(s));


But dont know how create the
List<myItem>

Answer
var list = 
    line1.Split(' ').
    Select((word, ix) => new myItem() { 
        ID = ix, 
        myValue = long.Parse(word) 
    }).
    ToList();
Comments