I have a situation where I need to output, via CSV (Tab-delimited), a dataset of information.
Problem is that if the column value contains a value, then it needs to be double quoted.
The value types, could range from Alpha-Numeric strings to DateTime formatted values.
Was wondering if there was easier way than this:
(string.IsNullOrWhiteSpace(evt.Name)?null:string.Format("\"{0}\"", evt.Name))
for each value that is being exported to string value.
Edit 2013-07-08 11:06 CST (modified 11:17 CST)
public string QuoteFormat(object val)
{
if(val != null) {
if(val == typeof(string))
val = (val as string).Replace("\"" , "\"\"");
return string.Format("\"{0}\"" , val);
}
return null;
}
char. LINQ is nice and fun, but it's not the best tool all the time.valis null in:return val.ToString()QuoteFormat(string val)living alongside ofQuoteFormat(object val). That will remove the need to check for the type.