tag:blogger.com,1999:blog-15136575.post3695033135935710106..comments2023-10-17T12:00:16.772+01:00Comments on Code rant: LINQ to CSVMike Hadlowhttp://www.blogger.com/profile/16441901713967254504noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-15136575.post-61883723456679157592011-06-09T09:42:24.944+01:002011-06-09T09:42:24.944+01:00Changed your ToCsvValue function to handle nulls, ...Changed your ToCsvValue function to handle nulls, and got rid of the custom Extension methods:<br /><br />private static string ToCsvValue(this T item)<br /> {<br /> if (item is string)<br /> {<br /> return string.Format("\"{0}\"", item.ToString().Replace("\"", "\\\"")); ;<br /> }<br /><br /> double dummy;<br /> if (item == null)<br /> return "";<br /><br /> if (double.TryParse(item.ToString(), out dummy))<br /> return string.Format("{0}", item);<br /><br /> return string.Format("\"{0}\"", item);<br /> }<br /><br />Also, regarding Scott's post on getting the headers: Change Append() to AppendLine() to avoid losing the first row of data!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-52428571948311022872011-02-02T22:29:39.051+00:002011-02-02T22:29:39.051+00:00if you want to had a line with the names of the li...if you want to had a line with the names of the linq properties do something like:<br /><br />if (UseHeader)<br /> {<br /> csvBuilder.Append(string.Join(",", (from a in properties select a.Name).ToArray()));<br /> }<br /><br />put this above the line that iterates over the ienumerableScottnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-39770851130021599492009-08-18T21:09:20.114+01:002009-08-18T21:09:20.114+01:00Hi Anonymous,
I can't remember now, but I may...Hi Anonymous,<br /><br />I can't remember now, but I may have written a little extension method for join. It's a simple matter to use string.Join() instead.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-40531193999528641122009-08-18T13:59:32.617+01:002009-08-18T13:59:32.617+01:00Hi,
I get the same error:
"No overload for ...Hi,<br /><br />I get the same error:<br /><br />"No overload for method 'Join' takes '1' arguments",Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-66392564556703203312009-07-28T18:57:05.881+01:002009-07-28T18:57:05.881+01:00Hi Mike
I'm also getting the error
"No o...Hi Mike<br />I'm also getting the error <br />"No overload for method 'Join' takes '1' arguments",<br />any idea why?<br /><br />ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-4911863608679222642008-07-24T14:42:00.000+01:002008-07-24T14:42:00.000+01:00Woops,I just saw your comment!Woops,<BR/><BR/>I just saw your comment!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-26545686682088519052008-07-24T14:41:00.000+01:002008-07-24T14:41:00.000+01:00Mike,You haven't thought about the scenario where ...Mike,<BR/><BR/>You haven't thought about the scenario where the thing you are converting has a string that has a comma in it!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-57555714594455396172008-07-09T09:11:00.000+01:002008-07-09T09:11:00.000+01:00Hi Luisfx,Sorry 'With' is an extension method on s...Hi Luisfx,<BR/><BR/>Sorry 'With' is an extension method on string in place of the string.format statement. Just change the line to:<BR/><BR/>return string.Format("\"{0}\"", item.ToString().Replace("\"", "\\\""));<BR/><BR/>And it should work fine.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-3739669171027386912008-07-08T17:12:00.000+01:002008-07-08T17:12:00.000+01:00Hi Mike, when I try to compile, I get an error "st...Hi Mike, when I try to compile, I get an error "string does not contain a definition for 'With'..." at "return "\"{0}\"".With(item) ... any ideas?<BR/><BR/>Luis.LuisFXhttps://www.blogger.com/profile/13337634696952285734noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-76297412603983071812008-06-06T09:23:00.000+01:002008-06-06T09:23:00.000+01:00Hi Ken,Thanks, that's a good suggestion. There are...Hi Ken,<BR/><BR/>Thanks, that's a good suggestion. <BR/><BR/>There are other things that I really should do like escape commas properly and new lines. It's not really the most robust CSV implementation at the moment.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-59171147694400344182008-06-06T08:45:00.000+01:002008-06-06T08:45:00.000+01:00Cool stuff.One worry on the date format though. i ...Cool stuff.<BR/><BR/>One worry on the date format though. i think it would be better to use a portable format (i.e - insensitive to mmdd/ddmm and to timezones)<BR/>so:<BR/>if (item is DateTime)<BR/>{<BR/> return string.Format("{0:u}", item);<BR/>}Anonymousnoreply@blogger.com