{"id":495,"date":"2011-09-01T11:05:03","date_gmt":"2011-09-01T09:05:03","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=495"},"modified":"2011-09-01T12:46:39","modified_gmt":"2011-09-01T10:46:39","slug":"extension-method-to-create-csv-from-list-collection","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/09\/extension-method-to-create-csv-from-list-collection\/","title":{"rendered":"Extension method to create CSV from List collection"},"content":{"rendered":"<p>A quick one to share:<\/p>\n<p>I needed to a method to quickly convert a List&lt;string&gt; to a plain comma delimited string. So I created this little Extension class:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public\u00a0static\u00a0class<\/span>\u00a0ListEx<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public\u00a0static\u00a0string<\/span>\u00a0ToCSVString(<span style=\"color: #0000ff;\">this\u00a0List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt;\u00a0l)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">StringBuilder\u00a0<\/span>sb\u00a0=\u00a0<span style=\"color: #0000ff;\">new\u00a0StringBuilder<\/span>();<br \/>\n<span style=\"color: #0000ff;\">if\u00a0<\/span>(l\u00a0!=\u00a0<span style=\"color: #0000ff;\">null<\/span>)<br \/>\n{<\/p>\n<p style=\"padding-left: 90px;\">l.ForEach(li\u00a0=&gt;\u00a0sb.Append(li\u00a0+\u00a0&#8220;,\u00a0&#8220;));<\/p>\n<p style=\"padding-left: 60px;\">}<br \/>\n<span style=\"color: #0000ff;\">return\u00a0<\/span>sb.ToString().Trim(&#8216;\u00a0&#8216;,\u00a0&#8216;,&#8217;);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Of course this is still very specific to string collections only. I could have created something that does the same to say a List&lt;T&gt; list that is more generic but that I&#8217;ll leave for another day.<\/p>\n<p>[Update]<\/p>\n<p>While I&#8217;m at it &#8211; here is the opposite method:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public static List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt; ToListFromCSVString(<span style=\"color: #0000ff;\">this string<\/span> s, <span style=\"color: #0000ff;\">bool<\/span> ordered = <span style=\"color: #0000ff;\">false<\/span>)<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt; list = <span style=\"color: #0000ff;\">new List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt;();<br \/>\n<span style=\"color: #0000ff;\">if<\/span> (s != <span style=\"color: #0000ff;\">null<\/span> &amp;&amp; s.Trim().Length &gt; 0)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">if<\/span> (ordered)<\/p>\n<p style=\"padding-left: 90px;\">list.AddRange((<span style=\"color: #800000;\">from<\/span> sitem <span style=\"color: #800000;\">in<\/span> s.Split(new char[] { &#8216;,&#8217; }, <span style=\"color: #0000ff;\">StringSplitOptions<\/span>.RemoveEmptyEntries)<br \/>\n<span style=\"color: #800000;\">orderby<\/span> sitem<br \/>\n<span style=\"color: #800000;\">select<\/span> sitem.Trim()).ToArray());<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">else<\/span><\/p>\n<p style=\"padding-left: 90px;\">list.AddRange((<span style=\"color: #800000;\">from<\/span> sitem <span style=\"color: #800000;\">in<\/span> s.Split(new char[] { &#8216;,&#8217; }, <span style=\"color: #0000ff;\">StringSplitOptions<\/span>.RemoveEmptyEntries)<br \/>\n<span style=\"color: #800000;\">select<\/span> sitem.Trim()).ToArray());<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">return<\/span> list;<\/p>\n<p>}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A quick one to share: I needed to a method to quickly convert a List&lt;string&gt; to a plain comma delimited string. So I created this little Extension class: public\u00a0static\u00a0class\u00a0ListEx { public\u00a0static\u00a0string\u00a0ToCSVString(this\u00a0List&lt;string&gt;\u00a0l) { StringBuilder\u00a0sb\u00a0=\u00a0new\u00a0StringBuilder(); if\u00a0(l\u00a0!=\u00a0null) { l.ForEach(li\u00a0=&gt;\u00a0sb.Append(li\u00a0+\u00a0&#8220;,\u00a0&#8220;)); } return\u00a0sb.ToString().Trim(&#8216;\u00a0&#8216;,\u00a0&#8216;,&#8217;); } } &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/09\/extension-method-to-create-csv-from-list-collection\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303,76,78,77,24],"class_list":["post-495","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development","tag-extension","tag-list","tag-method","tag-tip"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":7,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/495\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}