Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6816155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:53:31+00:00 2026-05-26T20:53:31+00:00

I am parsing a comma delimited set of data using LINQ. Within the LINQ

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:53:31+00:00Added an answer on May 26, 2026 at 8:53 pm

    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]
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im parsing the source of a website and Im using this regex: /page\.php\?id\=([0-9]*)\\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content) self.agent.page.content
I wrote a script parsing a .csv file in groovy using tokenize, which ended
I'm passing a comma delimited list of ids to a stored procedure as a
I am passing a comma-delimited list of values into a stored procedure. I need
I would like to be able to parse a non-empty, one-or-many element, comma-delimited (and
I am parsing a file of data whose fields are separated by pipes. The
I'm reading clipboard data coming from excel using var stream = (System.IO.Stream) ( Forms.Clipboard.GetDataObject()
I'm trying to perform some super simple parsing o log files, so I'm using
In order to store arbitrary data in jsTree nodes, I'm passing a custom attribute
I do a lot of file parsing, which involves parsing data types like decimal.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.