<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code.commongroove.com &#187; C#</title>
	<atom:link href="http://code.commongroove.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.commongroove.com</link>
	<description>C#, T-SQL, and general IT mojo</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:00:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C#: Export a List of Objects to CSV with LINQ, Attributes, and Generics</title>
		<link>http://code.commongroove.com/2012/01/25/c-export-a-list-of-objects-to-csv-with-linq-attributes-and-generics/</link>
		<comments>http://code.commongroove.com/2012/01/25/c-export-a-list-of-objects-to-csv-with-linq-attributes-and-generics/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 03:00:48 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Attributes]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=334</guid>
		<description><![CDATA[Have you ever needed to transform data from in-memory C# objects to a CSV flat-file of a specific format?  Here&#8217;s an easy way to get the job done with C#, LINQ, and Generics. First you need the Attribute you will &#8230; <a href="http://code.commongroove.com/2012/01/25/c-export-a-list-of-objects-to-csv-with-linq-attributes-and-generics/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have you ever needed to transform data from in-memory C# objects to a CSV flat-file of a specific format?  Here&#8217;s an easy way to get the job done with C#, LINQ, and Generics.</p>
<p>First you need the Attribute you will use to mark up the properties on your exportable class:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// The name of the column for the CSV</span><br />
<span style="color: #008080; font-style: italic;">/// generated by a list of objects with</span><br />
<span style="color: #008080; font-style: italic;">/// properties marked with this attribute,</span><br />
<span style="color: #008080; font-style: italic;">/// if Export is true. &nbsp;Uses Order to order</span><br />
<span style="color: #008080; font-style: italic;">/// the properties on export</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CsvColumnNameAttribute <span style="color: #008000;">:</span> Attribute<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Export <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Order <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> CsvColumnNameAttribute<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Export <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Order <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// so unordered columns are at the end</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></div>
<p>And here is how you use the attributes to mark up your class so you can export a list of instantiated objects:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyClassToExport<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>CsvColumnAttribute<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Activation Date&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> DateTime Date <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>CsvColumnAttribute<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Full Name&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> User <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>CsvColumnAttribute<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Account Type&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Level <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>CsvColumnAttribute<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Account Action&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Action <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>CsvColumnAttribute<span style="color: #008000;">&#40;</span>Export <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> DateTime Added <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Then you need to code to actually create the CSV using LINQ and Generics:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// Generate a CSV as a string from a list</span><br />
<span style="color: #008080; font-style: italic;">/// of objects that have the CsvColumnNameAttribute</span><br />
<span style="color: #008080; font-style: italic;">/// applied</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetCsv<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> csvDataObjects<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; PropertyInfo<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> propertyInfos <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperties</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">AppendLine</span><span style="color: #008000;">&#40;</span>GetCsvHeaderSorted<span style="color: #008000;">&#40;</span>propertyInfos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; csvDataObjects<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">ForEach</span><span style="color: #008000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">AppendLine</span><span style="color: #008000;">&#40;</span>GetCsvDataRowSorted<span style="color: #008000;">&#40;</span>d, propertyInfos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetCsvDataRowSorted<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T csvDataObject, PropertyInfo<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> propertyInfos<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> valuesSorted <span style="color: #008000;">=</span> propertyInfos<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>csvDataObject, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Attribute <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>CsvColumnNameAttribute<span style="color: #008000;">&#41;</span>Attribute<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttribute</span><span style="color: #008000;">&#40;</span>x, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>CsvColumnNameAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Attribute</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Attribute</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Export</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Attribute</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Order</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> GetPropertyValueAsString<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, valuesSorted<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetCsvHeaderSorted<span style="color: #008000;">&#40;</span>PropertyInfo<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> propertyInfos<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> headersSorted <span style="color: #008000;">=</span> propertyInfos<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span>CsvColumnNameAttribute<span style="color: #008000;">&#41;</span>Attribute<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttribute</span><span style="color: #008000;">&#40;</span>x, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>CsvColumnNameAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Export</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Order</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, headersSorted<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetPropertyValueAsString<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> propertyValue<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> propertyValueString<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyValue <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyValue <span style="color: #008000;">is</span> DateTime<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span>propertyValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;dd MMM yyyy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyValue <span style="color: #008000;">is</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> propertyValue<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyValue <span style="color: #008000;">is</span> <span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span>propertyValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;#.####&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// format as you need it</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyValue <span style="color: #008000;">is</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>propertyValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;#.####&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// format as you need it</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008080; font-style: italic;">// treat as a string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyValueString <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">+</span> propertyValue<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;&quot;</span>, <span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// quotes with 2 quotes</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> propertyValueString<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Once you have that code up in going in your application, you can export your list of objects to a CSV with just a couple lines of code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">// example usage</span><br />
var export <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>MyClassToExport<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008080; font-style: italic;">// TODO add items to list :)</span><br />
var csv <span style="color: #008000;">=</span> GetCsv<span style="color: #008000;">&#40;</span>export<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Hope this helps!  And let me know if you have any suggestions or improvements.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2012/01/25/c-export-a-list-of-objects-to-csv-with-linq-attributes-and-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# : String Extension to Parse Enums</title>
		<link>http://code.commongroove.com/2011/09/23/c-string-extension-to-parse-enums/</link>
		<comments>http://code.commongroove.com/2011/09/23/c-string-extension-to-parse-enums/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 15:30:48 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=320</guid>
		<description><![CDATA[It seems to me that there is too much typing involved in parsing a string as an Enum type. Here&#8217;s a quick string extension method to turn it into a simpler one liner: public static T EnumParse&#60;T&#62;&#40;this string input, bool &#8230; <a href="http://code.commongroove.com/2011/09/23/c-string-extension-to-parse-enums/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It seems to me that there is too much typing involved in parsing a string as an Enum type.  Here&#8217;s a quick string extension method to turn it into a simpler one liner:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T EnumParse<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">bool</span> ignoreCase <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, input, ignoreCase<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>With this extension, you can now do this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MyEnumValue&quot;</span><span style="color: #008000;">;</span><br />
MyEnum e <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">EnumParse</span><span style="color: #008000;">&lt;</span>MyEnum<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Seems shorter to me. <img src='http://www.commongroove.com/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/09/23/c-string-extension-to-parse-enums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC: Partially Secured Sites / Switching from HTTPS back to HTTP</title>
		<link>http://code.commongroove.com/2011/09/06/asp-net-mvc-partially-secured-sites-switching-from-https-back-to-http/</link>
		<comments>http://code.commongroove.com/2011/09/06/asp-net-mvc-partially-secured-sites-switching-from-https-back-to-http/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 16:47:20 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=300</guid>
		<description><![CDATA[Most sites out there have some portions that should be only served via HTTPS, while the remainder can be HTTP, such as account pages and content pages respectively.  This is sometimes called a &#8220;partially secured site.&#8221; Starting with MVC 2, &#8230; <a href="http://code.commongroove.com/2011/09/06/asp-net-mvc-partially-secured-sites-switching-from-https-back-to-http/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Most sites out there have some portions that should be only served via HTTPS, while the remainder can be HTTP, such as account pages and content pages respectively.  This is sometimes called a &#8220;partially secured site.&#8221;</p>
<p>Starting with MVC 2, you could decorate controllers and actions with the <strong>RequireHttps</strong> attribute, which would redirect non-secure GET requests to HTTPS.  Unfortunately, once you are in HTTPS, you won&#8217;t automatically switch back to HTTP for those actions that do not require it.</p>
<p>To do that, you can override <strong>OnActionExecuting</strong> in your base controller (to save you from having to reimplement the call in each of your actual controllers) and redirect the user.</p>
<p>Here&#8217;s the code</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">&#91;</span>RequireHttps<span style="color: #008000;">&#93;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult MySslAction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// HTTPS</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult MyNonSslAction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// HTTP</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuting<span style="color: #008000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsSecureConnection</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionDescriptor</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsDefined</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RequireHttpsAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// redirect to un-secured page</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> url <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://&quot;</span> <span style="color: #008000;">+</span> filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Host</span> <span style="color: #008000;">+</span> filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RawUrl</span><span style="color: #008000;">;</span><br />
filterContext<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Redirect</span><span style="color: #008000;">&#40;</span>url<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnActionExecuting</span><span style="color: #008000;">&#40;</span>filterContext<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/09/06/asp-net-mvc-partially-secured-sites-switching-from-https-back-to-http/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C#: String Extension to Replace Accented Characters</title>
		<link>http://code.commongroove.com/2011/04/29/c-string-extension-to-replace-accented-characters/</link>
		<comments>http://code.commongroove.com/2011/04/29/c-string-extension-to-replace-accented-characters/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 13:47:39 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=284</guid>
		<description><![CDATA[Have you ever wanted to replace &#8220;accented&#8221; characters in a string with their equivalent English character?  Here&#8217;s a string extension that replaces these diacritics within a string for C# 2.0 and up: public static string ReplaceDiacritics&#40;this string source&#41; &#123; string &#8230; <a href="http://code.commongroove.com/2011/04/29/c-string-extension-to-replace-accented-characters/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to replace &#8220;accented&#8221; characters in a string with their equivalent English character?  Here&#8217;s a string extension that replaces these diacritics within a string for C# 2.0 and up:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ReplaceDiacritics<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">string</span> source<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> sourceInFormD <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Normalize</span><span style="color: #008000;">&#40;</span>NormalizationForm<span style="color: #008000;">.</span><span style="color: #0000FF;">FormD</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var output <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span> c <span style="color: #0600FF; font-weight: bold;">in</span> sourceInFormD<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
UnicodeCategory uc <span style="color: #008000;">=</span> CharUnicodeInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUnicodeCategory</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>uc <span style="color: #008000;">!=</span> UnicodeCategory<span style="color: #008000;">.</span><span style="color: #0000FF;">NonSpacingMark</span><span style="color: #008000;">&#41;</span><br />
output<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>output<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Normalize</span><span style="color: #008000;">&#40;</span>NormalizationForm<span style="color: #008000;">.</span><span style="color: #0000FF;">FormC</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The extension replaces characters like &#8220;&#246;&#8221; with &#8220;o&#8221;, &#8220;&#232&#8243; with &#8220;e&#8221; and &#8220;&#241;&#8221; with &#8220;n&#8221;.  This is great for getting acceptable URLs or for auto-complete / type-ahead search boxes where you want to match on both the accented and non-accented characters.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/04/29/c-string-extension-to-replace-accented-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a SQL CLR User Defined Function</title>
		<link>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/</link>
		<comments>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 20:28:07 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=254</guid>
		<description><![CDATA[Starting with SQL Server 2005, Microsoft added the awesome ability to reference .Net assemblies from your T-SQL procedures.  Here&#8217;s a quick 5 step overview on how to get up and running with your code-based User Defined Function. 1) Create a &#8230; <a href="http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Starting with SQL Server 2005, Microsoft added the awesome ability to reference .Net assemblies from your T-SQL procedures.  Here&#8217;s a quick 5 step overview on how to get up and running with your code-based User Defined Function.</p>
<p>1) Create a project from the Visual C# SQL CLR Database Project template (just so you can get the correct references and get a test harness going).</p>
<p>2) Add a class containing your UDFs.  This one does a basic Regular Expression comparison:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyUdf<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">&#91;</span>Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">SqlServer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Server</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SqlFunction</span><span style="color: #008000;">&#40;</span>IsDeterministic <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> SqlBoolean FnRegExMatch<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> source, <span style="color: #6666cc; font-weight: bold;">string</span> pattern<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
Match m <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Match</span><span style="color: #008000;">&#40;</span>source, pattern,<br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInvariant</span> <span style="color: #008000;">|</span><br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span> <span style="color: #008000;">|</span><br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Singleline</span><br />
<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">return</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>3) Enable the CLR on your SQL Server instance through the configuration options:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">EXEC</span> sp_configure <span style="color: #ff0000;">'show advanced options'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
RECONFIGURE<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_configure <span style="color: #ff0000;">'clr enabled'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
RECONFIGURE<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_configure <span style="color: #ff0000;">'show advanced options'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'0'</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>4) Add the assembly and then create a reference to your UDF:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>MyUdfs<span style="color: #66cc66;">&#93;</span><br />
AUTHORIZATION <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\S</span>qlServerClr<span style="color: #000099; font-weight: bold;">\M</span>yUdf.dll'</span><br />
<span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> SAFE <span style="color: #808080; font-style: italic;">-- we only have &quot;safe&quot; calls in our lib</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>FnRegExMatch<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><br />
@<span style="color: #993333; font-weight: bold;">SOURCE</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@pattern nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> BIT<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">EXECUTE</span> <span style="color: #993333; font-weight: bold;">AS</span> CALLER<br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
EXTERNAL NAME <span style="color: #66cc66;">&#91;</span>MyUdf<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>FnRegExMatch<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>5) Now you should be able to call your function:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>FnRegExMatch<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'my test string'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\b</span>test<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Of course this is just the barest of essentials to get you going with SQL CLR UDFs, but I hope it helps!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Retrieve Image from Windows Clipboard via .Net C#</title>
		<link>http://code.commongroove.com/2009/11/02/retrieve-image-from-windows-clipboard-via-net-c/</link>
		<comments>http://code.commongroove.com/2009/11/02/retrieve-image-from-windows-clipboard-via-net-c/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 21:32:34 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/11/02/retrieve-image-from-windows-clipboard-via-.net-c.aspx</guid>
		<description><![CDATA[If you want to snag clipboard data from a different application into the context of your running .Net application, here&#8217;s how you can do it: private static Image _clipBoardImage = null; private static Image GetImageFromCopyPasteBuffer&#40;&#41; &#123; Thread t = new &#8230; <a href="http://code.commongroove.com/2009/11/02/retrieve-image-from-windows-clipboard-via-net-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you want to snag clipboard data from a different application into the context of your running .Net application, here&#8217;s how you can do it:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Image _clipBoardImage <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Image GetImageFromCopyPasteBuffer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
Thread t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ThreadStart<span style="color: #008000;">&#40;</span>GetClipboardBitmap<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
t<span style="color: #008000;">.</span><span style="color: #0000FF;">SetApartmentState</span><span style="color: #008000;">&#40;</span>ApartmentState<span style="color: #008000;">.</span><span style="color: #0000FF;">STA</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
t<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
t<span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">return</span> _clipBoardImage<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetClipboardBitmap<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
IDataObject data <span style="color: #008000;">=</span> Clipboard<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>data <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> <span style="color: #008000;">!</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataPresent</span><span style="color: #008000;">&#40;</span>DataFormats<span style="color: #008000;">.</span><span style="color: #0000FF;">Bitmap</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ApplicationException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;No clipboard image data was present.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
_clipBoardImage <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Image<span style="color: #008000;">&#41;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">GetData</span><span style="color: #008000;">&#40;</span>DataFormats<span style="color: #008000;">.</span><span style="color: #0000FF;">Bitmap</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/11/02/retrieve-image-from-windows-clipboard-via-net-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.Net Friend Assemblies</title>
		<link>http://code.commongroove.com/2009/10/21/net-friend-assemblies/</link>
		<comments>http://code.commongroove.com/2009/10/21/net-friend-assemblies/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 19:02:19 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/10/21/.net-friend-assemblies.aspx</guid>
		<description><![CDATA[Have you ever want to create a &#8220;friend&#8221; assembly while programming with the .Net framework?  They can be very useful for segmenting your codebase amongst different projects. Friend Assemblies allow an assembly to see the internal methods and properties of a &#8230; <a href="http://code.commongroove.com/2009/10/21/net-friend-assemblies/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have you ever want to create a &#8220;friend&#8221; assembly while programming with the .Net framework?  They can be very useful for segmenting your codebase amongst different projects.</p>
<p>Friend Assemblies allow an assembly to see the internal methods and properties of a different assembly by specifying an attribute on the primary assembly that specifies the assembly name of the friend.</p>
<p>The simplest way to do this is to add something like the following to your AssemblyInfo.cs file in your primary project:</p>
<p><font size="2">[</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">assembly</font></font><font size="2">: </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">InternalsVisibleTo</font></font><font size="2">(</font><font color="#a31515" size="2"><font color="#a31515" size="2">"my.friend.assembly"</font></font><font size="2">)]</font></p>
<p><font size="2">This would allow my.friend.assembly to have access to all of the primary project&#8217;s internals.</font></p>
<p>If you need to worry about security of the assemblies (I suppose we all should), then you should also consider using strong names in conjunction with the compiler attribute.</p>
<p>More information on .Net Friend Assemblies can be found here:</p>
<p><font face=""><a href="http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx">http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx</a></font></p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/10/21/net-friend-assemblies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript, UTF-8 Encoding: escape() vs. encodeURIComponent()</title>
		<link>http://code.commongroove.com/2009/08/06/javascript-utf-8-encoding-escape-vs-encodeuricomponent/</link>
		<comments>http://code.commongroove.com/2009/08/06/javascript-utf-8-encoding-escape-vs-encodeuricomponent/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 23:07:00 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/08/06/javascript-utf-8-encoding-escape-vs.-encodeuricomponent.aspx</guid>
		<description><![CDATA[ASP.Net web applications default to UTF-8 encoding.  Now, if you are using JavaScript to create or change URLs, you might run into an issue with the way the function &#8220;escape()&#8221; encodes characters if you are handling the decoding in your &#8230; <a href="http://code.commongroove.com/2009/08/06/javascript-utf-8-encoding-escape-vs-encodeuricomponent/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>ASP.Net web applications default to UTF-8 encoding.  Now, if you are using JavaScript to create or change URLs, you might run into an issue with the way the function &#8220;escape()&#8221; encodes characters if you are handling the decoding in your code behind via Request.QueryString.</p>
<p>To avoid ending up with &#8220;unkown&#8221; characters in your resultant string, use the JavaScript function &#8220;encodeURIComponent()&#8221; instead.  This should correctly encode your URLs for UTF-8.</p>
<p> </p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/08/06/javascript-utf-8-encoding-escape-vs-encodeuricomponent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert Hex to Readable String and Readable Text to Hex</title>
		<link>http://code.commongroove.com/2009/06/23/convert-hex-to-readable-string-and-readable-text-to-hex/</link>
		<comments>http://code.commongroove.com/2009/06/23/convert-hex-to-readable-string-and-readable-text-to-hex/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 22:24:16 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/06/23/convert-hex-to-readable-string-and-readable-text-to-hex.aspx</guid>
		<description><![CDATA[If you ever need to convert Hex data to readable strings or the reverse, try the following methods: private static readonly  char&#91;&#93; HexChars = &#34;0123456789ABCDEF&#34;.ToCharArray&#40;&#41;; private static string ConvertToHex&#40;string ascii&#41; &#123; if &#40;ascii == null&#41; return null; if &#40;ascii == &#8230; <a href="http://code.commongroove.com/2009/06/23/convert-hex-to-readable-string-and-readable-text-to-hex/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;">If you ever need to convert Hex data to readable strings or the reverse, try the following methods:</span></p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span>  <span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> HexChars <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0123456789ABCDEF&quot;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToCharArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ConvertToHex<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ascii<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ascii <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ascii <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> bytes <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>ascii<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
StringBuilder converted <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span>bytes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span> b <span style="color: #0600FF; font-weight: bold;">in</span> bytes<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
converted<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>HexChars<span style="color: #008000;">&#91;</span>b <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
converted<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>HexChars<span style="color: #008000;">&#91;</span>b <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span> 0xf<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">return</span> converted<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ConvertFromHex<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> hex<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>hex <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>hex <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>hex<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ApplicationException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hex string length should be divisble by 2: &quot;</span> <span style="color: #008000;">+</span> hex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> bytes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span>hex<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> bytes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
bytes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>hex<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">+</span> hex<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span>, NumberStyles<span style="color: #008000;">.</span><span style="color: #0000FF;">HexNumber</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #6666cc; font-weight: bold;">string</span> converted <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>bytes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>converted<span style="color: #008000;">&#91;</span>converted<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\0</span>'</span><span style="color: #008000;">&#41;</span><br />
converted <span style="color: #008000;">=</span> converted<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>converted<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">return</span> converted<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p><span style="font-size: small;">I have found this is very useful for parsing binary data from Active Directory sources such as &#8220;csvde&#8221;. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;">Hope this helps you out!<span style="mso-spacerun: yes;"> </span>Let me know if you know of a better/faster method – it is always appreciated.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/06/23/convert-hex-to-readable-string-and-readable-text-to-hex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read a CSV file with Regular Expressions in .Net</title>
		<link>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/</link>
		<comments>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:51:17 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/06/23/read-a-csv-file-with-regular-expressions-in-.net.aspx</guid>
		<description><![CDATA[Here’s how you can read a CSV file using Regular Expressions in .Net: &#60;/p&#62; public static DataTable GetDataTableFromCsvFile&#40;string file&#41; &#123; // Where the CSV data goes DataTable dt = new DataTable&#40;&#34;CsvData&#34;&#41;; // The pattern used to parse the CSV const &#8230; <a href="http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;">Here’s how you can read a CSV file using Regular Expressions in .Net:</span></p>
<p><span style="line-height: 115%;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;">
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable GetDataTableFromCsvFile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> file<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// Where the CSV data goes</span><br />
DataTable dt <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;CsvData&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// The pattern used to parse the CSV</span><br />
<span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> csvPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;,(?=(?:[^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>[^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>)*(?![^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>))&quot;</span><span style="color: #008000;">;</span><br />
Regex csvRegex <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Regex<span style="color: #008000;">&#40;</span>csvPattern<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Read all lines in the file</span><br />
<span style="color: #008080; font-style: italic;">// (not great for large files)</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> fileLines <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllLines</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Get the column headers</span><br />
<span style="color: #008080; font-style: italic;">// (assumes first row has headers and that</span><br />
<span style="color: #008080; font-style: italic;">//  each column contains string values and</span><br />
<span style="color: #008080; font-style: italic;">//  that each column name is unique)</span><br />
<br />
Dictionary<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>int, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> headers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>int, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> headerValues <span style="color: #008000;">=</span> csvRegex<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span>fileLines<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> headerValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
headers<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i, headerValues<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
dt<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> DataColumn<span style="color: #008000;">&#40;</span>headerValues<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, <span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Then add the the rest of the lines</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> k <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> k <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> fileLines<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> k<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
DataRow dr <span style="color: #008000;">=</span> dt<span style="color: #008000;">.</span><span style="color: #0000FF;">NewRow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #6666cc; font-weight: bold;">string</span> line <span style="color: #008000;">=</span> fileLines<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> cols <span style="color: #008000;">=</span> csvRegex<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span>line<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> cols<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> header <span style="color: #008000;">=</span> headers<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> cols<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// remove quotes around the field</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
dr<span style="color: #008000;">&#91;</span>header<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
dt<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>dr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">return</span> dt<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Of course, you will need to add in error catching and handling as well.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

