4

My code works great, as long as there aren't any commas in the data.

IEnumerable<Account> AccountItems = from line in File.ReadAllLines(filePath).Skip(1)
                let columns = line.Split(',')
                select new Account
                {
                    AccountName = columns[0],
                    BKAccountID = columns[1],
                    Brand = columns[2],
                    FirstOE = columns[3],
                    LastOE = columns[4]
                };

But the output includes data with commas, and wraps the data in double quotes when there is a comma in the data. I'm not sure if I can still use LINQ to do this.

Acme Health Care,{C2F9A7DD-0000-0000-0000-8B06859016AD},"Data With, LLC",2/4/2013,2/18/2013
1
  • 1
    Regex here would get very complex very quickly. It's better to use the .NET libraries to handle this. Commented Feb 19, 2013 at 19:58

1 Answer 1

7

Take a look at this question: Reading CSV files using C#

TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv");
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData) 
{
    //Processing row
    string[] fields = parser.ReadFields();
    foreach (string field in fields) 
    {
        //TODO: Process field
    }
}
parser.Close();

No need to reinvent the wheel when .NET can hold your hand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.