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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ye Myat Aung
  • 1,783
  • 11
  • 31
  • 49

3 Answers3

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.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • Will it affect the performance if the input data is quite large? – Ye Myat Aung Feb 01 '12 at 06:21
  • Potentially, I don't have any data on performance myself. You can run your own tests to see. – Kyle Trauberman Feb 01 '12 at 06:23
  • 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? – Ye Myat Aung Feb 01 '12 at 06:39
  • `string.Join(",", lineValues.Skip(1));` – Sergey Vyacheslavovich Brunov Feb 01 '12 at 06:57
  • @Ye Myat Aung `lineValues` is a `List`. 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. – Kyle Trauberman Feb 01 '12 at 07:20
  • Thank you for the explanation. But I want to skip the first row of te input data at the beginning before I do any calculations (because it contains characters). – Ye Myat Aung Feb 01 '12 at 08:08
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.

eouw0o83hf
  • 9,438
  • 5
  • 53
  • 75
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.

Community
  • 1
  • 1
cwharris
  • 17,835
  • 4
  • 44
  • 64