1

I was trying to add the strings to the List and export the list as csv file but the result I got is not the way I want. Here is the code -

List<string> values = new List<string>();
using (StreamReader sr = new StreamReader(filePath))
{
    while (sr.Peek() != -1)
    {
        string line = sr.ReadLine();
        List<string> lineValues = line.Split(',').ToList();
        var tempMinInt = 1;
        var tempValue = 1;
        var tempValInt = Convert.ToInt32(lineValues[4]);
        if (tempValInt % 60 != 0)
        {
            tempMinInt = (tempValInt / 60) + 1;
            tempValue = tempMinInt * 30;
        }
        else
        {
            tempMinInt = tempValInt / 60;
            tempValue = tempMinInt * 30;
        }


        values.Add(lineValues + "," + tempValue.ToString());

    }
}

Here is the sample input data:

33083,2011-12-19 05:17:57+06:30,98590149,1876,258
33084,2011-12-19 05:22:28+06:30,98590149,1876,69
33085,2011-12-19 05:23:45+06:30,98590149,1876,151
33086,2011-12-19 05:30:21+06:30,98590149,1876,58
33087,2011-12-19 06:44:19+06:30,949826259,1876,66

And here is the output data:

System.Collections.Generic.List`1[System.String],150
System.Collections.Generic.List`1[System.String],60
System.Collections.Generic.List`1[System.String],90
System.Collections.Generic.List`1[System.String],30
System.Collections.Generic.List`1[System.String],60

Please advice. Thank you.

2
  • Also, you should check out File.ReadAllLines in the System.IO namespace. It make importing files line-by-line much easier. Commented Feb 1, 2012 at 6:20
  • It also make it synchronous, so you shouldn't use that for large files. Commented Feb 1, 2012 at 6:22

3 Answers 3

5

List<T> does not override ToString, so you'll need to do the leg work here.

You can use string.Join to combine the values of the list into a comma separated string:

string.Join(",", lineValues.ToArray());

Your last line of code posted above will become:

values.Add(string.Join(",", lineValues.ToArray()) + "," + tempValue.ToString());

http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

Alternatively, you can just use line in this code snippet, as lineValues is built from line and you aren't modifying any of the values.

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

6 Comments

Will it affect the performance if the input data is quite large?
Potentially, I don't have any data on performance myself. You can run your own tests to see.
It works! But could you tell me if there's any difference between lineValues.ToArray() and just lineValues? And also, if I want to skip the first row of the input data, how can I do it?
string.Join(",", lineValues.Skip(1));
@Ye Myat Aung lineValues is a List<string>. The .ToArray method copies the data to a string[] array which the string.Join method takes as an argument. Serge is correct. You can use .Skip(1) to skip the first row. You'll still need to call .ToArray afterwards.
|
2

.NET is trying to turn a List of strings into a string, and it doesn't know how to do this implicitly. I think you should check out string.Join, and use it like:

values.Add(string.Join(",", lineValues) + "," + tempValue.ToString());

This will turn the list of strings into a comma-separated string.

Comments

0

What you're looking for is string.Join(",", lineValues)

Try this:

values.Add(string.Join(",", lineValues) + "," + tempValue.ToString());

Also, this won't work if any of those line values contain characters that need to be escaped.

See here for a solution to escaping characters.

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.