Jed
Jed

Reputation: 10887

How to create a string[] within a LINQ query via Regex.Split()

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?

Upvotes: 4

Views: 997

Answers (2)

Reed Copsey
Reed Copsey

Reputation: 564413

You can use the let clause to only call this once:

var rows = from a in dataSplit
    let splitValues = Regex.Split(a, ",")
    select new {
        Name    = splitValues[0],
        ID      = splitValues[1],
        Val     = splitValues[2],
        Time    = splitValues[3],
        Date    = splitValues[4]
    };

As a side note - if you're just splitting based on a specific character or string, and not an expression, you should just use String.Split:

string[] dataSplit = data.Split(new[] {Environment.NewLine}, StringSplitOptions.None);

var rows = from a in dataSplit
    let splitValues = a.Split(",")
    select new {
        Name    = splitValues[0],
        ID      = splitValues[1],
        Val     = splitValues[2],
        Time    = splitValues[3],
        Date    = splitValues[4]
    };

Upvotes: 1

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

You can store a subexpression in a variable with the let clause.

var rows = from a in dataSplit
    let splitResult = Regex.Split(a, ",")
    select new {
        Name    = splitResult [0],
        ID      = splitResult [1],
        Description = splitResult [2],
        Time    = splitResult [3],
        Date    = splitResult [4]
    };

Upvotes: 4

Related Questions