I am parsing a comma delimited set of data using LINQ. Within the LINQ query I call Regex.Split multiple times in order to parse comma delimited items. I am trying to figure out how I can avoid calling the .Split() method multiple times, but am failing to figure it out.
Example:
// Sample data
string data = @"A,1,Foo1,14:03:08,14/11/11,
A,2,Foo2,11:51:11,09/11/11,
A,3,Foo3,11:51:11, 09/11/11,
A,4,Foo4,12:11:13,09/11/11,
A,5,Foo5,12:23:02,13/11/11,
A,6,Foo6,15:37:58,11/11/11";
// Add each line of data into an array
string[] dataSplit = Regex.Split(data,"\r\n");
// Create an anon object for each line item
var rows = from a in dataSplit
select new {
Name = Regex.Split(a, ",")[0],
ID = Regex.Split(a, ",")[1],
Val = Regex.Split(a, ",")[2],
Time = Regex.Split(a, ",")[3],
Date = Regex.Split(a, ",")[4]
};
Notice in the LINQ query, I call the Regex.Split in order to determine the index value for each line item. Intuitively, it seems to me that calling the .Split() for each anon prop is unnecessary overhead.
How can I create a variable within the LINQ query that splits the line in scope such that when I set the property of the anon object, I do not have to call the Regex.Split() method?