Reputation: 1853
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.
Upvotes: 1
Views: 1444
Reputation: 25684
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.
Upvotes: 5
Reputation: 18125
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.
Upvotes: 0
Reputation: 9588
.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.
Upvotes: 2