<?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>Meatball&#039;s Guide to .NET &#187; LinqPad</title>
	<atom:link href="http://rogerpence.com/blog/index.php/archives/tag/linqpad/feed" rel="self" type="application/rss+xml" />
	<link>http://rogerpence.com/blog</link>
	<description>A dogma-free guide to making real-world sense of .NET</description>
	<lastBuildDate>Wed, 23 Jun 2010 16:15:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Linq to create an ADO.NET connection string</title>
		<link>http://rogerpence.com/blog/index.php/archives/197</link>
		<comments>http://rogerpence.com/blog/index.php/archives/197#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:57:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[LinqPad]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/index.php/archives/197</guid>
		<description><![CDATA[I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. 
I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, [...]]]></description>
			<content:encoded><![CDATA[<p>I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. </p>
<p>I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, but still I was intrigued. I set out last night to see how Linq could negate the need for For/Each. </p>
<p>I set out to find a way to create ADO.NET connection strings. In the past, I would have probably used foreach to iterate over a collection using StringBuilder and String.Format to build up the connection string. I dispensed with all that old-school thought and created the connection string as shown below in Figure 1. As a bonus, I also learned (thanks to a little prompting from <a href="http://www.jetbrains.com/resharper/">Resharper</a>) a shortcut for initializing a collection.</p>
<p>The Linq code in lines 10 and 11 is where the cool stuff happens. Each element from the dictionary is selected to create a value pair and the Linq Aggregate() operator is used to concatenate all of the value pairs. All very sweet! </p>
<div class="sourceCodeHeading">Figure 1. Using Linq to create an ADO.NET connection string</div>
<div id="Id1106513593SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">var o = new Dictionary&lt; string, string &gt;()</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">	{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">		{ &quot;Data Source&quot;,           &quot;DUFFSQLSERVER2008&quot; },</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">		{ &quot;Initial Catalog&quot;,       &quot;Chinook&quot; },</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">		{ &quot;Persist Security Info&quot;, &quot;True&quot; },</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">		{ &quot;User ID&quot;,               &quot;xxxxx&quot; },</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">		{ &quot;Password&quot;,              &quot;xxxxx&quot; }</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code">	};    </span></pre>
<pre class="even"><span class="ln">  9</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">var ConnectionString = o.Select( i =&gt; i.Key + &quot;=&quot; + i.Value ).</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">                         Aggregate( ( a, b ) =&gt; a + &quot;;&quot; + b );</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code"></span></pre>
<pre class="even"><span class="ln"> 13</span><span class="code">Console.WriteLine( ConnectionString );</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1106513593SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1106513593SourceCode' );">Show copy-friendly code</a></div>
<p>I used <a href="http://www.linqpad.net/">LinqPad</a> to create and test this code. I just started using LinqPad and can’t believe I ever coded without it. Beyond being a great test bed for Linq, it is a superb snippet compiler. LinqPad is a free download and should be in every coder’s toolbox. </p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad1.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="linqpad" border="0" alt="linqpad" src="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad-thumb1.jpg" width="775" height="489" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/197/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
