<?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>PeterKellner.net &#187; LINQ</title>
	<atom:link href="http://peterkellner.net/category/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focussed, JavaScript (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Tue, 07 Feb 2012 21:14:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Microsoft To Add Auto-Compiled LINQ Queries to Entity Framework V.Next!</title>
		<link>http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/</link>
		<comments>http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 01:57:03 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[.NET 4.5]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/</guid>
		<description><![CDATA[&#160;  In May of 2009 I discovered some significant performance problems that I blogged about.&#160; In summary, I had tracked it down to the issue of LINQ2SQ having to create a full expression tree on every instantiation of a LINQ2SQL query.&#160; I’m not a compiler write kind of guy but do respect the complexity [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <p>In May of 2009 I discovered some significant performance problems that I <a href="http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/">blogged</a> about.&#160; In summary, I had tracked it down to the issue of LINQ2SQ having to create a full expression tree on every instantiation of a LINQ2SQL query.&#160; I’m not a compiler write kind of guy but do respect the complexity of that and doing things like building expression trees, but still this really sucked.&#160; Using the compile syntax in LINQ2SQL is very awkward, and IMHO takes all the fun out of using LINQ2SQL.&#160; If you don’t remember my post, here is the graph showing the evil happening.</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/wp/wp/wp-content/uploads/2011/10/image6.png" width="285" height="173" /></a></p>  <p>So, what is a Microsoft MVP to do?&#160; I complained to everyone at Microsoft I knew.&#160; I made this my mission for about a year.&#160; I was told time after time that the problem was hugely complicated and no one was working on making it any faster.&#160; I just could not believe it because I felt this was such a barrier to success to <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ2SQL</a> type technology (now <a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx">Entity Framework</a> really) that Microsoft just could not ignore my findings.&#160; I was actually surprised that I was the only one screaming about this.</p>  <p>Well, quietly, <a href="http://blogs.msdn.com/b/adonet/archive/2011/10/18/how-we-talk-about-ef-and-its-future-versions.aspx">Microsoft has announced that in .Net Framework 4.5, Entity Framework will include “Auto-Compiled LINQ Queries”</a>.&#160; This is awesome!&#160;&#160; When it comes out, I’ll be testing and it giving feedback.</p>  <p>For now, I’m a very happy camper.</p>  <p>&#160;</p>  <p><a href="http://blogs.msdn.com/b/adonet/archive/2011/10/18/how-we-talk-about-ef-and-its-future-versions.aspx"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/wp/wp/wp-content/uploads/2011/10/image7.png" width="882" height="474" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Cool / Unexpected Refactoring around .Net C# Locking Issue</title>
		<link>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/</link>
		<comments>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 14:32:12 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Lock]]></category>
		<category><![CDATA[ReSharper]]></category>
		<category><![CDATA[Refactor]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/</guid>
		<description><![CDATA[I’m constantly amazed by the insightfulness of ReSharper’s suggested refactorings (ReSharper is a Visual Studio Addin from JetBrains I use with C#). Today, I’ve been working on a threading problem where I’m getting crashes based on what seems like not proper locking across threads (they usually show up as some type of ugly update object [...]]]></description>
			<content:encoded><![CDATA[<p>I’m constantly amazed by the insightfulness of <a href="http://www.jetbrains.com/resharper/index.html">ReSharper’s</a> suggested refactorings (ReSharper is a <a href="http://www.microsoft.com/visualstudio/en-us/visual-studio-2010-launch?WT.mc_id=SEARCH&amp;WT.srch=1">Visual Studio</a> Addin from <a href="http://www.jetbrains.com/index.html">JetBrains</a> I use with C#). Today, I’ve been working on a threading problem where I’m getting crashes based on what seems like not proper locking across threads (they usually show up as some type of ugly update object or enum error).</p>  <p>My code starts like this:</p>  <p>&#160;</p>  <div>   <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty { get; set; }</pre>
</div>

<div>&#160;</div>
<span id="more-1347"></span>

<div>
  <br /></div>

<p>Then, I try wrapping the updates with a lock as follows (removing the set because I do it someplace else now)</p>

<p>&#160;</p>

<div id="codeSnippetWrapper">
  <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty<br />{<br />    get<br />    {<br />        <span style="color: #0000ff">lock</span> (lockDbProgressReportProperty)<br />        {<br />            <span style="color: #0000ff">return</span> _dbProgressReportList;<br />        }<br />    }<br />}</pre>

  <br /></div>

<p>&#160;</p>

<p>I then realize that I need to copy the data because it may change while the caller prints it so I decide to return a temporary copy of the data as follows:</p>

<div id="codeSnippetWrapper">
  <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;DbProgressReport&gt; DbProgressReportProperty<br />{<br />    get<br />    {<br />        <span style="color: #0000ff">lock</span> (lockDbProgressReportProperty)<br />        {<br />            var dbProgressReportsNew = <span style="color: #0000ff">new</span> List&lt;DbProgressReport&gt;();<br />            <span style="color: #0000ff">if</span> (_dbProgressReportList != <span style="color: #0000ff">null</span>)<br />            {<br />                <span style="color: #008000">// make temp copy to avoid locking issues on read</span><br />                <span style="color: #0000ff">foreach</span> (var rec <span style="color: #0000ff">in</span> _dbProgressReportList)<br />                {<br />                    dbProgressReportsNew.Add(rec);<br />                }<br />            }<br />            <span style="color: #0000ff">return</span> dbProgressReportsNew;<br />        }<br />    }<br />}</pre>

  <br /></div>

<p>I then notice that ReSharper has a suggestion.</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/ACoolUnexpectedRefactor.NetCLockingIssue_69E6/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/ACoolUnexpectedRefactor.NetCLockingIssue_69E6/image_thumb.png" width="512" height="268" /></a> </p>

<p></p>

<p>Taking the suggestion, Resharper changes the code to:</p>

<div>
  <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">lock</span> (lockDbProgressReportProperty)<br />{<br />    <span style="color: #008000">// make temp copy to avoid locking issues on </span><br />    <span style="color: #0000ff">return</span> _dbProgressReportList.ToList();<br />}</pre>
</div>

<div>&#160;</div>

<div>I expected a bunch of AddRange type stuff, but ReSharper figured out that ToList() would do everything I needed!</div>

<div>&#160;</div>

<div>Very cool.</div>

<div>&#160;</div>

<div>Hope this helps.</div>

<div>
  <br /></div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/29/resharper-refactor-locking-linq/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ReSharper 5.0&#8217;s LINQ Refactoring Continues to be Amazing!</title>
		<link>http://peterkellner.net/2010/04/21/linq-refactor-resharper-aspnet-dropdownlist/</link>
		<comments>http://peterkellner.net/2010/04/21/linq-refactor-resharper-aspnet-dropdownlist/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 00:46:00 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/04/21/linq-refactor-resharper-aspnet-dropdownlist/</guid>
		<description><![CDATA[&#160;  In this post, we’ll take a straight forward procedure based set of code and convert it to LINQ using a ReSharper from JetBrains suggestion.&#160;&#160; I’ve found that in general, when I do things with foreach syntax, there is often a better way in Linq to do this.&#160; The better does not jump out [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <p>In this post, we’ll take a straight forward procedure based set of code and convert it to <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a> using a <a href="http://www.jetbrains.com/resharper/whatsnew/">ReSharper</a> from <a href="http://www.jetbrains.com/index.html">JetBrains</a> suggestion.&#160;&#160; I’ve found that in general, when I do things with foreach syntax, there is often a better way in Linq to do this.&#160; The better does not jump out at me sometimes, however with ReSharper, it is often a button click away.</p> <span id="more-1274"></span>  <p>The initial code is as follows:</p>  <pre class="csharpcode"><span class="kwrd">int</span> selectedIndex = 0;
<span class="kwrd">foreach</span> (var rec <span class="kwrd">in</span> codeCampStringDictionary)
{
    <span class="kwrd">if</span> (rec.Key == codeCampYear)
    {
        <span class="kwrd">break</span>;
    }
    selectedIndex++;
}</pre>

<p>And, <a href="http://www.jetbrains.com/resharper/">ReSharper</a> is going to suggest turning it into:</p>

<pre class="csharpcode"><span class="kwrd">int</span> selectedIndex = codeCampStringDictionary.TakeWhile(rec =&gt; rec.Key != codeCampYear).Count();</pre>
<style type="text/css">





.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Simply Amazing!&#160; <a href="http://www.hookedonlinq.com/TakeWhileOperator.ashx">TakeWhile</a> had not even occurred to me.</p>

<p>(All while getting the <a href="http://www.siliconvalley-codecamp.com/">Silicon Valley Code Camp</a> Site Ready for our V5 Event, October 9th and 10th, 2010)</p>

<p>So, to explain the problem a little better, on the <a href="http://www.siliconvalley-codecamp.com/">Silicon Valley Code Camp</a> web site (which, by the way is scheduled for October 9th and 10th, 2010 this year), we have an ASP.NET <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.aspx">DropDownList</a> in the upper right hand corner that the user can switch between different years. That is, say they want to see all the sessions in 2008’s code camp, they can simply change the dropdown and the site completely updates.&#160; When they do this, we don’t want to re-read the data table that contains the code camp years, we simply want to process the data that has already been stored in the <a href="http://msdn.microsoft.com/en-us/library/ms972976.aspx">ViewState</a> of the DropDownList and use that to figure out the key value of the table entry for the code camp year they selected.</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/ReS.0sLINQRefactoringContinuestobeAmazin_F9C9/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/ReS.0sLINQRefactoringContinuestobeAmazin_F9C9/image_thumb.png" width="632" height="172" /></a> </p>

<p>OK, if you didn’t follow all that, it really doesn’t matter.&#160; This blog post is simply about how clever ReSharper is and how much I enjoy what the tool does.&#160; It’s nice to have a tool teach me things!</p>

<p>The complete commented code is below that includes some comments about how it actually works.&#160; I’m looking forward to several people posting comments letting me know that there is even a simpler way to do this.&#160;&#160; I love my job!</p>

<p>&#160;</p>

<div id="codeSnippetWrapper">
  <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">// Create a dictionary out of values in a DropDownList.  That is,</span><br /><span style="color: #008000">// the DropDownList has a Key Value which is it's primary key</span><br /><span style="color: #008000">// and a text value which is the Code Camp Year.</span><br />Dictionary&lt;<span style="color: #0000ff">int</span>, <span style="color: #0000ff">string</span>&gt; codeCampStringDictionary =<br />    DropDownListCodeCampYearID.Items.Cast&lt;ListItem&gt;().<br />        OrderBy(a =&gt; a.Value).<br />        ToDictionary(itemdd =&gt; Convert.ToInt32(itemdd.Value),<br />                     itemdd =&gt; itemdd.Text);<br /><br /><span style="color: #008000">// Then, loop through the values in the dictionary and figure out</span><br /><span style="color: #008000">// the offset of a particular value.  That is, if there are 3 values</span><br /><span style="color: #008000">// in the list of 2008,2009,2010 which have primary keys of 3,4,5</span><br /><span style="color: #008000">// we want to know, that given codeCampYear is 4, then selectedIndex </span><br /><span style="color: #008000">// is the second value and should be 2.</span><br /><span style="color: #0000ff">int</span> selectedIndex = codeCampStringDictionary.TakeWhile<br />    (rec =&gt; rec.Key != codeCampYear).Count();<br /><br /><span style="color: #0000ff">string</span> dateString = Utils.GetCodeCampDateStringByCodeCampYearId(codeCampYear);<br />DropDownListCodeCampYearID.SelectedIndex = selectedIndex;<br />HeaderId1.Text = String.Format(<span style="color: #006080">&quot;Saturday and Sunday, {0}&quot;</span>, dateString);<br /></pre>

  <br /></div>

<p>Let the comments roll! </p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/04/21/linq-refactor-resharper-aspnet-dropdownlist/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Small LINQ Trick for Converting List&lt;String&gt; to String Text List</title>
		<link>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/</link>
		<comments>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:58:28 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/</guid>
		<description><![CDATA[Here is a shorthand way for converting a list of strings defined as follows:   List&#60;string&#62; strings = new List&#60;string&#62;
 
	{
		&#34;a&#34;,&#34;b&#34;,&#34;c&#34;

	};



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a shorthand way for converting a list of strings defined as follows:</p>  <pre class="csharpcode"> List&lt;<span class="kwrd">string</span>&gt; strings = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;
 
	{
		<span class="str">&quot;a&quot;</span>,<span class="str">&quot;b&quot;</span>,<span class="str">&quot;c&quot;</span>

	};</pre>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>To something that looks like:</p>
<span id="more-525"></span>

<p>“a:b:c:”</p>

<p>Here is the trick using the foreach syntax:</p>

<pre class="csharpcode"> var stringBuilder = <span class="kwrd">new</span> StringBuilder();
 strings.ForEach(a=&gt;stringBuilder.AppendFormat(<span class="str">&quot;{0}:&quot;</span>,a));</pre>

<p>&#160; </p>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Adding A Sessions Page That Includes a Query Parameter In Silverlight VS2010 Beta2 (Article 6 of 7)</title>
		<link>http://peterkellner.net/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/</link>
		<comments>http://peterkellner.net/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 23:22:47 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[RIA Services]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[VS2010]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/</guid>
		<description><![CDATA[&#160;              &#160;        Title Of Each Article        Video Included With Each Post                  [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <table width="90%"><tbody>     <tr>       <td width="70">&#160;</td>        <td>Title Of Each Article</td>        <td style="width: 100px" width="150">Video Included With Each Post</td>     </tr>      <tr>       <td width="70">Part 1</td>        <td><a href="/2010/01/20/riaservices-silverlight-4-tutorial-svcc-part1of7-introduction/">Introduction To RIA Services In Silverlight (This Article)</a></td>        <td style="width: 100px" width="150">7 Minutes</td>     </tr>      <tr>       <td width="70">Part 2</td>        <td><a href="/2010/01/25/basic-ria-services-and-datagrid-with-vs-2010-tooling-article-2-of-7/">Basic RIA Services And DataGrid With&#160; VS 2010 Tooling</a></td>        <td style="width: 100px" width="150">14 Minutes</td>     </tr>      <tr>       <td width="70">Part 3</td>        <td><a href="/2010/01/23/adding-a-datagrid-with-connect-the-dots-databinding-in-vs-2010-article-3-of-7/">Adding A DataGrid With Connect The Dots DataBinding in VS 2010</a></td>        <td style="width: 100px" width="150">13 Minutes</td>     </tr>      <tr>       <td width="70">Part 4</td>        <td><a href="/2010/01/25/adding-a-navigation-page-to-a-silverlight-business-application-template-article-4-of-7/">Adding a Navigation Page to a Silverlight Business Application Template</a></td>        <td style="width: 100px" width="150">11 Minutes</td>     </tr>      <tr>       <td width="70">Part 5</td>        <td><a href="/2010/01/25/adding-speakers-page-template-with-converters-in-visual-studio-2010-beta2-article-5-of-7/">Adding Speakers Page Template With Converters In Visual Studio 2010 Beta2</a></td>        <td style="width: 100px" width="150">11 Minutes</td>     </tr>      <tr>       <td width="70">Part 6</td>        <td><a href="/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/">Adding A Sessions Page That Includes a Query Parameter In Silverlight VS2010 Beta2</a></td>        <td style="width: 100px" width="150">10 Minutes</td>     </tr>      <tr>       <td width="70">Part 7</td>        <td><a href="/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/">Basic Authentication and Authorization In RIA Services</a></td>        <td style="width: 100px" width="150">5 Minutes</td>     </tr>   </tbody></table>  <br />  <br />  <br /><img src="http://video.peterkellner.net/video/RIAServicesBayNet20090120/P6_AddingSessionsPageWithRequestParam_Thumb.jpg" alt="media" /><br />
  <br />  <br />  <p>In this section, we will talk about what happens when the users presses the “Sessions” hyperlink from the speakers page.&#160; If you remember from the last article, we used a special converter on the Speakers.xaml page and bound that to the hyperlink titled sessions as shown below.</p> <span id="more-482"></span>  <p>&#160;</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/AddingASessionsPageThatIncludesaQueryPar_DF36/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AddingASessionsPageThatIncludesaQueryPar_DF36/image_thumb.png" width="376" height="323" /></a></p>  <p>&#160;</p>  <p>When this link is clicked, the navigation created is as follows:</p>  <pre class="csharpcode">&#160;</pre>

<pre class="csharpcode"><a href="http://localhost:9951/Presentation1TestPage.aspx#/Sessions?SpeakerId=903">http://localhost:9951/Presentation1TestPage.aspx#/Sessions?SpeakerId=903</a></pre>

<!-- code formatted by http://manoli.net/csharpformat/ -->
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
</style>
<div class="csharpcode">
<pre class="alt"><span class="rem">// Executes when the user navigates to this page.</span></pre>
<pre><span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnNavigatedTo(NavigationEventArgs e)</pre>
<pre class="alt">{</pre>
<pre>    <span class="rem">//Handle SpeakerId</span></pre>
<pre class="alt">    var qs = NavigationContext.QueryString;</pre>
<pre>    <span class="kwrd">if</span> (qs.ContainsKey(<span class="str">"SpeakerId"</span>))</pre>
<pre class="alt">    {</pre>
<pre>        <span class="kwrd">this</span>.sessionsOverviewDomainDataSource.FilterDescriptors =</pre>
<pre class="alt">            <span class="kwrd">new</span> FilterDescriptorCollection();</pre>
<pre>        <span class="kwrd">this</span>.sessionsOverviewDomainDataSource.FilterDescriptors.Add(</pre>
<pre class="alt">              <span class="kwrd">new</span> FilterDescriptor(<span class="str">"AttendeeId"</span>,</pre>
<pre>                  FilterOperator.IsEqualTo, qs[<span class="str">"SpeakerId"</span>]));</pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>

<p>This simply adds a filter to the current DomainDataSource that filters the column AttendeeId with the passed in query parameter SpeakerId.</p>

<p>&#160;</p>

<p>The resulting page looks as follows assuming <a href="http://www.siliconvalley-codecamp.com/Sessions.aspx?ForceSortBySessionTime=true&amp;id=151">Peter Kellner’s Session link</a> was clicked.</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/AddingASessionsPageThatIncludesaQueryPar_DF36/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AddingASessionsPageThatIncludesaQueryPar_DF36/image_thumb_3.png" width="523" height="207" /></a></p>

<p>&#160;</p>

<p>The video referenced in this article goes into building this page in more details, but fundamentally, the major point is just the query parameter.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://video.peterkellner.net/video/RIAServicesBayNet20090120/P6_AddingSessionsPageWithRequestParam.flv" length="1" type="video/x-flv"/>
	</item>
		<item>
		<title>Most Amazing ReFactor Using ReSharper EAP 5.0 I&#8217;ve Seen!</title>
		<link>http://peterkellner.net/2009/12/04/refactor-csharp-linq/</link>
		<comments>http://peterkellner.net/2009/12/04/refactor-csharp-linq/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 18:27:56 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/12/04/refactor-csharp-linq/</guid>
		<description><![CDATA[&#160;  This one is just to amazing to not blog about.&#160; I’ve been a die heart ReSharper user for quite a while and recently have started using their early access versions.&#160; Primarily because they seem to have added a lot of LINQ support which I use heavily now.&#160; I had the following code before [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <p>This one is just to amazing to not blog about.&#160; I’ve been a die heart <a href="http://www.jetbrains.com/resharper/">ReSharper</a> user for quite a while and recently have started using their early access versions.&#160; Primarily because they seem to have added a lot of <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a> support which I use heavily now.&#160; I had the following code before the refactor.&#160; I have to admit, it’s a little .Net 1.1/2.0 ish, but what can I say, I’m a production coder and it did the job.</p>  <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> GetTransitTime(<span class="kwrd">string</span> parcelToCheck)
{
    <span class="kwrd">int</span> transitTime = 0;
    <span class="kwrd">foreach</span> (var parcelSCAC <span class="kwrd">in</span> Constant.UPS_SCAC_MAP)
    {
        <span class="kwrd">if</span> (parcelSCAC.Key.Equals(parcelToCheck))
        {
            transitTime = Constant.UPS_TRANSIT_TIME[parcelSCAC.Value];
            <span class="kwrd">break</span>;
        }
    }
    <span class="kwrd">return</span> transitTime;
}</pre>
<span id="more-392"></span>

<p>I noticed the little helper icon making a suggestion (<a href="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+Early+Access+Program">ReSharper</a> Icon that is) so I hovered over to see what it wanted.</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/MostAmazingReFactorUsingReSharp.0IveSeen_9324/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/MostAmazingReFactorUsingReSharp.0IveSeen_9324/image_thumb.png" width="438" height="282" /></a></p>

<p>And, I chose what it suggested, and wow!&#160; check this out.&#160;&#160; The way I should have coded it in the first place.</p>

<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> GetTransitTime(<span class="kwrd">string</span> parcelToCheck)
{
    <span class="kwrd">return</span> (from parcelSCAC <span class="kwrd">in</span> Constant.UPS_SCAC_MAP
            <span class="kwrd">where</span> parcelSCAC.Key.Equals(parcelToCheck)
            select Constant.UPS_TRANSIT_TIME[parcelSCAC.Value]).FirstOrDefault();
}</pre>

<p>
  <br /></p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>All I can say is I love it.&#160;&#160; If you don’t use ReSharper, drop what you are doing and go out and buy it!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/12/04/refactor-csharp-linq/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Converting From System.Data.Linq.Binary to String and Back</title>
		<link>http://peterkellner.net/2009/11/08/csharp-linqbinary-tostring-andback/</link>
		<comments>http://peterkellner.net/2009/11/08/csharp-linqbinary-tostring-andback/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 17:49:58 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/11/08/csharp-linqbinary-tostring-andback/</guid>
		<description><![CDATA[ Just a quick post in case anyone is wasting 10 minutes figuring out how to do this.&#160; For me, this came up because in Sql Server 2008, NVARCHAR(MAX) is 4000 characters and I needed more.&#160; The recommended datatype to use for more is VARBINARY.&#160; When I did that, LINQ2SQL converted that type to Linq.Binary. [...]]]></description>
			<content:encoded><![CDATA[<p> Just a quick post in case anyone is wasting 10 minutes figuring out how to do this.&#160; For me, this came up because in <a href="http://www.microsoft.com/sqlserver/2008/en/us/overview.aspx">Sql Server 2008</a>, NVARCHAR(MAX) is 4000 characters and I needed more.&#160; The recommended datatype to use for more is VARBINARY.&#160; When I did that, LINQ2SQL converted that type to <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.binary.aspx">Linq.Binary</a>. (using <a href="http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx">encoding</a>)</p>  <p>So, in my C# code, here is what you need to convert to the Linq.Binary:</p> <span id="more-379"></span>  <p>Linq.Binary carrierMatrix =    <br />&#160;&#160; <span class="kwrd">new</span> ASCIIEncoding().GetBytes(myString);</p>  <pre class="csharpcode">And, to go back from Linq.Binary:</pre>

<pre class="csharpcode"><span class="kwrd">string</span> carrierMatrixString = Encoding.ASCII.GetString(carrierMatrix.ToArray());<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style></pre>

<p><font size="2" face="Consolas">Hope this help!</font></p>

<pre class="csharpcode">&#160;</pre>

<p>
  <br /></p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/11/08/csharp-linqbinary-tostring-andback/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using LINQ to Convert an Array Into a Generic List with C#</title>
		<link>http://peterkellner.net/2009/10/14/linq-convert-array-to-list/</link>
		<comments>http://peterkellner.net/2009/10/14/linq-convert-array-to-list/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 00:35:45 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/10/14/linq-convert-array-to-list/</guid>
		<description><![CDATA[This is just going to be a short post, but I bet it’s something I do a large number of times so I thought I’d blog it.&#160; Say you get back from something like a web service an array of objects.   In my case this:  cmRateResult[] cmRateResults = TransiteUtils.MakeRateRequest(_cmRateRequest);

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: [...]]]></description>
			<content:encoded><![CDATA[<p>This is just going to be a short post, but I bet it’s something I do a large number of times so I thought I’d blog it.&#160; Say you get back from something like a web service an array of objects.</p> <span id="more-364"></span>  <p>In my case this:</p>  <pre class="csharpcode">cmRateResult[] cmRateResults = TransiteUtils.MakeRateRequest(_cmRateRequest);</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Then, I want to convert that into</p>

<p>List&lt;cmRateResult]</p>

<p>We all know the foreach way of doing it, about 5 lines of code</p>

<pre class="csharpcode">var cmRateResultsList = <span class="kwrd">new</span> List&lt;cmRateResult&gt;(cmRateResults.Length);
 <span class="kwrd">foreach</span> (var r <span class="kwrd">in</span> cmRateResults)
 {
      cmRateResultsList.Add(r);
 }</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Or, my preferred way is to use the LINQ for syntax as follows:</p>

<pre class="csharpcode">var recs = (from data <span class="kwrd">in</span> cmRateResults select data).ToList();</pre>

<p>But, it’s nicer (IMHO) to do it with LINQ chaining, though exactly the same. here it is:</p>

<pre class="csharpcode">var recs = cmRateResults.Select(data =&gt; data)).ToList()</pre>

<p>Hope this helps!</p>

<pre class="csharpcode">&#160;</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/10/14/linq-convert-array-to-list/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>LINQPad Help For Creating Nasty Group by Query in Native SQL</title>
		<link>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/</link>
		<comments>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 16:51:21 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/</guid>
		<description><![CDATA[ I often speak very highly of LINQ and also LINQPad.&#160; This morning, I was struggling with some sql that would let me do a count by DateTime while stripping out the time portion.&#160; That is, I just want to know how many entries are in the table for each Date (regardless of what time).&#160; [...]]]></description>
			<content:encoded><![CDATA[<p> I often speak very highly of <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a> and also <a href="http://www.linqpad.net/">LINQPad</a>.&#160; This morning, I was struggling with some sql that would let me do a count by DateTime while stripping out the time portion.&#160; That is, I just want to know how many entries are in the table for each Date (regardless of what time).&#160; I tried lots of solutions I got from search, and they all gave not correct results, usually involving Casting and other non fun sql programming constructs.</p> <span id="more-361"></span>  <p>It occurred to me I could do it in LINQ, then grab out the SQL it generated.&#160; The tool?&#160; LINQPad of course.&#160; So, here is what I typed into LINQPad:</p>  <pre class="csharpcode">(from data <span class="kwrd">in</span> Attendees
let dateNoTime = 
    <span class="kwrd">new</span> DateTime
     (data.CreationDate.Value.Year,
      data.CreationDate.Value.Month,
      data.CreationDate.Value.Day)
orderby dateNoTime
group data by dateNoTime into g
<span class="kwrd">where</span> g.Count() &gt; 0
select <span class="kwrd">new</span> 
    {
    g.Key,
    Number_Registered = g.Count() 
    }).OrderByDescending(f=&gt;f.Key)</pre>

<p>
  <br /></p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<pre class="csharpcode">Then, After pasting that into LINQPad, choosing the SQL tab on the bottom, it gives me this:</pre>

<pre class="csharpcode"><span class="kwrd">SELECT</span> [t2].[value3] <span class="kwrd">AS</span> [<span class="kwrd">Key</span>], [t2].[value2] <span class="kwrd">AS</span> [Number_Registered]
<span class="kwrd">FROM</span> (
<span class="kwrd">SELECT</span> <span class="kwrd">COUNT</span>(*) <span class="kwrd">AS</span> [<span class="kwrd">value</span>], <span class="kwrd">COUNT</span>(*) <span class="kwrd">AS</span> [value2], [t1].[<span class="kwrd">value</span>] 
          <span class="kwrd">AS</span> [value3]
<span class="kwrd">FROM</span> (
    <span class="kwrd">SELECT</span> <span class="kwrd">CONVERT</span>
         (DATETIME, <span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(2), DATEPART(<span class="kwrd">Month</span>, [t0].[CreationDate])) 
           + (<span class="str">'/'</span> + (<span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(2), DATEPART(<span class="kwrd">Day</span>, [t0].[CreationDate])) +
              (<span class="str">'/'</span> + <span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(4), DATEPART(<span class="kwrd">Year</span>, [t0].[CreationDate]))))), 101) 
                    <span class="kwrd">AS</span> [<span class="kwrd">value</span>]
        <span class="kwrd">FROM</span> [Attendees] <span class="kwrd">AS</span> [t0]
        ) <span class="kwrd">AS</span> [t1]
    <span class="kwrd">GROUP</span> <span class="kwrd">BY</span> [t1].[<span class="kwrd">value</span>]
    ) <span class="kwrd">AS</span> [t2]
<span class="kwrd">WHERE</span> [t2].[<span class="kwrd">value</span>] &gt; @p0
<span class="kwrd">ORDER</span> <span class="kwrd">BY</span> [t2].[value3] DESC</pre>

<p>
  <br /></p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>And, the nice screen shot of LINQPad</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/LINQPadHelpForCreatingNastyGroupbyQueryi_875D/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/LINQPadHelpForCreatingNastyGroupbyQueryi_875D/image_thumb.png" width="397" height="421" /></a></p>

<p>And, it works perfect!&#160; Even the TSql generated looks pretty nice.&#160; <a href="http://msdn.microsoft.com/en-us/library/ms177673.aspx">Group By</a>, <a href="http://msdn.microsoft.com/en-us/library/ms187928.aspx">Convert</a>, <a href="http://msdn.microsoft.com/en-us/library/ms174420.aspx">DatePart</a> and everything. Somehow, I feel like I’ve cheated, but I’m good with that in this case.</p>

<p>Hope this helps!</p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some Really Cool LINQ to Help Performance and Show Line Thickness</title>
		<link>http://peterkellner.net/2009/09/18/linq-groupby-grouplist-uniquesort-mapping/</link>
		<comments>http://peterkellner.net/2009/09/18/linq-groupby-grouplist-uniquesort-mapping/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 17:49:55 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/09/18/linq-groupby-grouplist-uniquesort-mapping/</guid>
		<description><![CDATA[I’ve done a bunch of Lat/Long type mapping programs over the years and one of the problems always ends up around performance.&#160; A common problem is that you keep redrawing the same line over and over and over.&#160; The pixels you are drawing don’t get any darker so all you are doing is wasting time.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve done a bunch of Lat/Long type mapping programs over the years and one of the problems always ends up around performance.&#160; A common problem is that you keep redrawing the same line over and over and over.&#160; The pixels you are drawing don’t get any darker so all you are doing is wasting time.&#160; In my current project, we were drawing approximately 1500 lines when we really only had about 150 unique lines.&#160; I’ve always known how to solve this problem with a bunch of thrown together hacked up code, but now, <a href="http://www.hookedonlinq.com/LinqToSQL5MinuteOVerview.ashx">LINQ</a> gives me a very clean way to do it.</p>  <p>So, the problem is you have a record that looks like this:</p>  <pre class="csharpcode"> <span class="kwrd">public</span> <span class="kwrd">class</span> LoadSegmentInfoFlat
        {
            <span class="kwrd">public</span> <span class="kwrd">double</span>? OriginLattitude { get; set; }
            <span class="kwrd">public</span> <span class="kwrd">double</span>? OriginLongitude { get; set; }
            <span class="kwrd">public</span> <span class="kwrd">double</span>? DestinationLattitude { get; set; }
            <span class="kwrd">public</span> <span class="kwrd">double</span>? DestinationLongitude { get; set; }
            <span class="kwrd">public</span> <span class="kwrd">string</span> Color { get; set; }
        }</pre>
<span id="more-357"></span>

<p>You’ve created a list of these and defined it as follows:</p>

<pre class="csharpcode">var loadSegmentInfoFlats = <span class="kwrd">new</span> List&lt;LoadController.LoadSegmentInfoFlat&gt;();</pre>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Now, you want to create a unique list of Lattitude, Longitude Origins and Destinations by color.</p>

<p>The LINQ query that does the trick is a simple group by that creates an anonymous result (which is our answer).&#160; Here is the LINQ query (thanks to my friend in the <a href="http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/3846fd8f-7616-4eb2-a024-7b5dda508a04">MSDN LINQ Forums</a>, Lignzhi Sun).</p>

<pre class="csharpcode">var segments = from a <span class="kwrd">in</span> loadSegmentInfoFlats
               group a by
                   <span class="kwrd">new</span>
                       {
                           a.OriginLattitude,
                           a.OriginLongitude,
                           a.DestinationLattitude,
                           a.DestinationLongitude,
                           a.Color
                       }
               into grouplist
                   select
                   <span class="kwrd">new</span>
                       {
                           grouplist.Key.OriginLattitude,
                           grouplist.Key.OriginLongitude,
                           grouplist.Key.DestinationLattitude,
                           grouplist.Key.DestinationLongitude,
                           grouplist.Key.Color,
                           Count = grouplist.Count()
                       };</pre>

<p>&#160;</p>

<p>The resulting segments in Visual Studio 2008 debugger are as follows:</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/SomeReallyCoolLINQtoHelpPerformanceandSh_931C/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/SomeReallyCoolLINQtoHelpPerformanceandSh_931C/image_thumb.png" width="492" height="321" /></a></p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Hope this helps!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/09/18/linq-groupby-grouplist-uniquesort-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using LINQ2SQL, Use Group By To Get List of Top Occuring Values</title>
		<link>http://peterkellner.net/2009/09/03/linq2sql-groupby-having-orderbydescending-delegate/</link>
		<comments>http://peterkellner.net/2009/09/03/linq2sql-groupby-having-orderbydescending-delegate/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 23:05:45 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/09/03/linq2sql-groupby-having-orderbydescending-delegate/</guid>
		<description><![CDATA[ So, I’m really enjoying using LINQ and specifically LINQ2SQL.&#160; I’ve got a current problem where I want to get from a long list of loads (potentially hundreds per day), a short list of days in reverse order, that have a certain number or more of loads.   So, How to do this?&#160; Well, [...]]]></description>
			<content:encoded><![CDATA[<p> So, I’m really enjoying using LINQ and specifically <a href="http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/threads">LINQ2SQL</a>.&#160; I’ve got a current problem where I want to get from a long list of loads (potentially hundreds per day), a short list of days in reverse order, that have a certain number or more of loads.</p> <span id="more-350"></span>  <p>So, How to do this?&#160; Well, first, here is the answer:</p>  <div class="csharpcode">   <pre class="alt"><span class="lnum">   1:  </span>       <span class="rem">/// &lt;summary&gt;</span></pre>

  <pre><span class="lnum">   2:  </span>        <span class="rem">/// Returns a list of days that have a loadCount &gt; loadCount</span></pre>

  <pre class="alt"><span class="lnum">   3:  </span>        <span class="rem">/// &lt;/summary&gt;</span></pre>

  <pre><span class="lnum">   4:  </span>        <span class="rem">/// &lt;param name=&quot;days&quot;&gt;&lt;/param&gt;</span></pre>

  <pre class="alt"><span class="lnum">   5:  </span>        <span class="rem">/// &lt;param name=&quot;loadCount&quot;&gt;&lt;/param&gt;</span></pre>

  <pre><span class="lnum">   6:  </span>        <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

  <pre class="alt"><span class="lnum">   7:  </span>        <span class="kwrd">public</span> List&lt;DateTime&gt; GetLastDaysWithLoadsGreaterThan(<span class="kwrd">int</span> days,<span class="kwrd">int</span> loadCount)</pre>

  <pre><span class="lnum">   8:  </span>        {</pre>

  <pre class="alt"><span class="lnum">   9:  </span>            var dateGroups = (from load <span class="kwrd">in</span> _meta.Load</pre>

  <pre><span class="lnum">  10:  </span>                              orderby load.DateCreated</pre>

  <pre class="alt"><span class="lnum">  11:  </span>                              group load by load.DateCreated</pre>

  <pre><span class="lnum">  12:  </span>                              into g</pre>

  <pre class="alt"><span class="lnum">  13:  </span>                                  <span class="kwrd">where</span> g.Count() &gt; loadCount</pre>

  <pre><span class="lnum">  14:  </span>                                  select <span class="kwrd">new</span></pre>

  <pre class="alt"><span class="lnum">  15:  </span>                                             {</pre>

  <pre><span class="lnum">  16:  </span>                                                 g.Key,</pre>

  <pre class="alt"><span class="lnum">  17:  </span>                                                 cnt = g.Count()</pre>

  <pre><span class="lnum">  18:  </span>                                             }).Take(days).OrderByDescending(f =&gt; f.Key);</pre>

  <pre class="alt"><span class="lnum">  19:  </span>            </pre>

  <pre><span class="lnum">  20:  </span>            var listDates = dateGroups.Select(a =&gt; a.Key).ToList();</pre>

  <pre class="alt"><span class="lnum">  21:  </span>            </pre>

  <pre><span class="lnum">  22:  </span>            <span class="kwrd">return</span> listDates;</pre>

  <pre class="alt"><span class="lnum">  23:  </span>        }</pre>
</div>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>And now, a short explanation.</p>

<p>First, the obvious I thought would have been if I did orderby load.DateCreated descending, that I would not have had to reverse the list at the end.&#160; It did not work and I’m not sure why. Luckily, I can sort the list at the very end which is a small penalty since my lists are not that large.</p>

<p>Notice the groupby and the where clause are doing the work.&#160; The resulting anonymous method solves the problem of getting the data out.</p>

<p>Line 20 is one of my favorite tricks to avoid 4 or 5 lines of code.</p>

<p>And of course, the SQL is a lot simpler, but not type safe.</p>

<div class="csharpcode">
  <pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">SELECT</span> datecreated,</pre>

  <pre><span class="lnum">   2:  </span>       <span class="kwrd">count</span>(*)</pre>

  <pre class="alt"><span class="lnum">   3:  </span><span class="kwrd">FROM</span> dbo.[<span class="kwrd">Load</span>]</pre>

  <pre><span class="lnum">   4:  </span><span class="kwrd">group</span> <span class="kwrd">by</span> datecreated</pre>

  <pre class="alt"><span class="lnum">   5:  </span><span class="kwrd">having</span> <span class="kwrd">count</span>(*) &gt; 200</pre>

  <pre><span class="lnum">   6:  </span><span class="kwrd">order</span> <span class="kwrd">by</span> datecreated <span class="kwrd">desc</span></pre>

  <pre class="alt"><span class="lnum">   7:  </span>  </pre>
</div>

<div class="csharpcode">&#160;</div>

<div class="csharpcode">That’s it!&#160; hope this helps.</div>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/09/03/linq2sql-groupby-having-orderbydescending-delegate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interesting LINQ side affect, Pass in empty List&lt;int&gt; using Contains and Get Where 0 == 1. Hmm</title>
		<link>http://peterkellner.net/2009/06/14/linq2sql-gotcha-generic-list/</link>
		<comments>http://peterkellner.net/2009/06/14/linq2sql-gotcha-generic-list/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 15:21:18 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/14/linq2sql-gotcha-generic-list/</guid>
		<description><![CDATA[We’ve built a data access layer on top of LINQ2SQL for dynamically building the layer we call for data access.&#160; It’s convenient because we pass in a query object as a parameter that has a bunch of nullable variables in it.&#160; Here is kind of what it looks like:       [...]]]></description>
			<content:encoded><![CDATA[<p>We’ve built a data access layer on top of LINQ2SQL for dynamically building the layer we call for data access.&#160; It’s convenient because we pass in a query object as a parameter that has a bunch of nullable variables in it.&#160; Here is kind of what it looks like:</p>  <div class="csharpcode">   <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> CodeGenTestQuery : QueryBase</pre>

  <pre>    {</pre>

  <pre class="alt">        <span class="kwrd">public</span> <span class="kwrd">int</span>? Id { get; set; }</pre>

  <pre>        <span class="kwrd">public</span> List&lt;<span class="kwrd">int</span>&gt; Ids { get; set; }</pre>

  <pre class="alt">        <span class="kwrd">public</span> <span class="kwrd">bool</span>? IsStarred { get; set; }</pre>

  <pre>        ...</pre>
</div>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>If I pass an empty list of int’s into Ids, I have a line of code that build the query as follows:</p>
<span id="more-323"></span>

<pre class="csharpcode"><span class="kwrd">if</span> (query.Ids != <span class="kwrd">null</span>) baseQuery = baseQuery.Where(a =&gt; query.Ids.Contains(a.Id));</pre>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>This works fine as long as a pass into ids either null, or a list that contains more than one object. However, if I pass in an empty list like this:</p>

<pre class="csharpcode"><span class="kwrd">new</span> CodeGenTestQuery
    {
         Ids = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;() { }, MyAge = 2
     }</pre>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Then, the result of the LINQ query is it creates a where clause that says something like this:</p>

<p><span class="kwrd">SELECT</span> [t0].[Id],</p>

<p>&#160;&#160;&#160;&#160;&#160;&#160; [t0].[IsStarred]</p>

<p><span class="kwrd">FROM</span> [dbo].[CodeGenTest] <span class="kwrd">AS</span> [t0]</p>

<p><span class="kwrd">WHERE</span> ([t0].[MyAge] = 2) <span class="kwrd">AND</span></p>

<p>&#160;&#160;&#160;&#160;&#160; (0 = 1)</p>

<p>Which means you will not get the MyAge == 2 record ever</p>

<p>So, I changed my c# as follows and now I get what I expect.</p>

<pre class="csharpcode"><span class="kwrd">if</span> (query.Ids != <span class="kwrd">null</span>)
            {
                <span class="kwrd">if</span> (query.Ids.Count &gt; 0)
                {
                    baseQuery = baseQuery.Where(a =&gt; query.Ids.Contains(a.Id));
                }
}</pre>

<p>
  <br /></p>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style><style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>I can see this kind of thinking on the <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ2SQL</a> team, but it’s a little insidious if you ask me.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/14/linq2sql-gotcha-generic-list/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Combing LINQPAD with LINQ In Action From Manning</title>
		<link>http://peterkellner.net/2009/06/09/linqpad-wooley-manning-linqinaction/</link>
		<comments>http://peterkellner.net/2009/06/09/linqpad-wooley-manning-linqinaction/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 16:08:39 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/09/linqpad-wooley-manning-linqinaction/</guid>
		<description><![CDATA[ Last night, I had the honor of having dinner with three other VSLive speakers including Jim Wooley, one of the authors of the awesome book on LINQ titled LINQ in Action.&#160; Jim is a wealth of knowledge as well as very entertaining.&#160; He had an interesting analogy for what using try/catch statements to handle [...]]]></description>
			<content:encoded><![CDATA[<p> Last night, I had the honor of having dinner with three other <a href="http://vslive.com/2009/lasvegas/speakers.aspx#chairs">VSLive</a> speakers including <a href="http://twitter.com/linqkinq">Jim Wooley</a>, one of the authors of the awesome book on LINQ titled <a href="http://www.amazon.com/exec/obidos/ASIN/1933988169/petkelsblo-20">LINQ in Action</a>.&#160; Jim is a wealth of knowledge as well as very entertaining.&#160; He had an interesting analogy for what using try/catch statements to handle expected issues.&#160; I’ll leave it up to you to post to his twitter account and find out what it was.&#160; it’s just a little off color and I don’t want to be rejected from search engines.</p> <span id="more-322"></span>  <p>At anyrate, I’m now following <a href="http://twitter.com/linqkinq">Jim’s Twitter feed</a> and he recently mentioned he included all the examples from his book in <a href="http://www.linqpad.net/">LINQPAD</a>.&#160; I just downloaded them.&#160; This is just too cool!&#160; He wrote a blog post about it here:&#160; <a title="http://www.thinqlinq.com/Default/LINQ-In-Action-Samples-available-in-LINQPad.aspx" href="http://www.thinqlinq.com/Default/LINQ-In-Action-Samples-available-in-LINQPad.aspx">http://www.thinqlinq.com/Default/LINQ-In-Action-Samples-available-in-LINQPad.aspx</a>&#160; Here is what it looks like in LINQPAD</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/CombingLINQPADwithLINQInActionFromMannin_807A/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/CombingLINQPADwithLINQInActionFromMannin_807A/image_thumb.png" width="593" height="355" /></a></p>  <p>Hope this helps!!!&#160; It’s huge for me.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/09/linqpad-wooley-manning-linqinaction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>780 Requests Per Second Verses 110, You Really Need to Compile your LINQ to SQL (LINQ2SQL) Queries</title>
		<link>http://peterkellner.net/2009/06/08/linq2sql-uncompiled-verses-compiled-iis-performance-aspnet/</link>
		<comments>http://peterkellner.net/2009/06/08/linq2sql-uncompiled-verses-compiled-iis-performance-aspnet/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 05:21:49 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/08/linq2sql-uncompiled-verses-compiled-iis-performance-aspnet/</guid>
		<description><![CDATA[*Note 10/22/2011 (2+ years later):&#160; Microsoft is fixing this problem in EntityFramework 4.5!&#160; See my post here about it: http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/  &#160;  


   If you have a problem like this involving ASP.NET, LINQ, EF, etc., consulting services are available.  We love these kind of problems.  Contact Peter Kellner and his [...]]]></description>
			<content:encoded><![CDATA[<p><font style="background-color: #ffff00">*Note 10/22/2011 (2+ years later):&#160; Microsoft is fixing this problem in EntityFramework 4.5!&#160; See my post here about it:</font> <a href="http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/"><font style="background-color: #ffff00">http://peterkellner.net/2011/10/22/microsoft-to-add-auto-compiled-linq-queries-to-entity-framework-v-next/</font></a></p>  <p>&#160;</p>  

<div>
  <div class="peterkellnerpromo"> <a href="/contactus" target="_blank">If you have a problem like this involving ASP.NET, LINQ, EF, etc., consulting services are available.  We love these kind of problems.  Contact Peter Kellner and his associates <span>here</span></a>. </div> 
  <div>

<p>So, I’ve been on kind of a rant lately about how slow <a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx">LINQ2SQL</a> is if you don’t compile your queries before executing them.&#160; To be fair, if you are doing Windows Forms Programming, WPF or <a href="http://silverlight.net/">Silverlight</a> it really does not have much impact.&#160; The reason is that a very complex LINQ query may take 50 milliseconds (1/20th of a second).&#160; No big deal if you just have a dozen or so of them to do.&#160; The story changes though if you are using LINQ2SQL in a web environment that has limited CPU resources.&#160; That is, unless you have unlimited money, if it takes more than one web server to handle your load, your throwing away money by using <a href="http://jdconley.com/blog/archive/2007/11/28/linq-to-sql-surprise-performance-hit.aspx">uncompiled LINQ2SQL</a>.</p>  <p>So, to put some more substance behind my claims, I’ve written a small test application using Visual Studio 2008 that compares the performance of using LINQ2SQL compiled verses non-compiled on a trivial web page.</p>   
</div>
</div>

 
<span id="more-318"></span>
    <p>Here are some assumptions:</p>  <ul>   <li>My Notebook Computer Simulates a real world web server (Lenovo W500, Vista64, IIS7) </li>    <li>All Web Pages do 7 LINQ2SQL executions (Heading speak at&#160; VSLive in Vegas so 7 seemed right) </li>    <li>All Web Requests Do Only LINQ2SQL, no images, css, JavaScript or anything else </li> </ul>  <p>OK, maybe my assumptions are not so good, but I believe overall I’m making a good point so I’m going to keep going rather than just say “take my word for it, trust me”.&#160; So, I’m attaching the web project here that let’s you recreate all this so you can do your own tests.&#160; I think you’ll need Visual Studio Team Suite to do these tests.&#160; If you don’t have that, you can at least look at the code.&#160; The guts are in LINQUtils.cs.&#160; I’m going to show the relevant code below.</p>  <p>But before that, here is the Visual Studio 2008 Project Code if you want to try it yourself: </p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:b2b30af6-cfd4-413d-abeb-71e63d4120bf" class="wlWriterSmartContent">   <p>Download: <a href="/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/LINQ2SQL_CompiledQueryWebTest.zip" target="_blank">Project Zip File</a></p> </div>  <p>First, The Results</p>  <table border="0" cellspacing="0" cellpadding="2" width="400"><tbody>     <tr>       <td valign="top" width="200"><a href="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/notcompiled.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="notcompiled" border="0" alt="notcompiled" src="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/notcompiled_thumb.png" width="412" height="237" /></a> </td>        <td valign="top" width="200"><a href="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/notcompiled1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="notcompiled1" border="0" alt="notcompiled1" src="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/notcompiled1_thumb.png" width="250" height="236" /></a> </td>     </tr>      <tr>       <td valign="top" width="200"><a href="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/compiled.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="compiled" border="0" alt="compiled" src="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/compiled_thumb.png" width="411" height="219" /></a> </td>        <td valign="top" width="200"><a href="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/780RequestsPerSecondVerses110YouReallyNe_F121/image_thumb.png" width="247" height="221" /></a> </td>     </tr>   </tbody></table>  <p>The code is very straight forward.&#160; Below is the code for the LINQ2SQL Not Compiled Test:</p>  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 90%; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ProcessLinqQueryNotCompiled()
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  2: {
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">if</span> (meta == <span style="color: #0000ff">null</span>)
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  4:     {
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  5:         meta = <span style="color: #0000ff">new</span> DataClassesDataContext();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  6:     }
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  7: 
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  8:     Stopwatch stopWatch = <span style="color: #0000ff">new</span> Stopwatch();
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  9:     stopWatch.Start();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 10: 
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 11:     <span style="color: #0000ff">int</span> retCnt = 0;
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 12:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; iterations; i++)
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 13:     {
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 14:         <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> companyTypeIdToFind = 1;
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 15:         var q3 = from dataCo <span style="color: #0000ff">in</span> meta.Companies
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 16:                  where (from dataCt <span style="color: #0000ff">in</span> meta.CompanyCompanyTypes
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 17:                         where dataCt.CompanyTypeId == companyTypeIdToFind
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 18:                         select dataCt.CompanyId).Contains(dataCo.Id)
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 19:                  select dataCo;
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 20: 
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 21:         var newList = q3.ToList();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 22:         retCnt = newList.Count;
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 23:     }
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 24:     stopWatch.Stop();
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 25:     <span style="color: #0000ff">double</span> milliSecondsPerIteration = Convert.ToDouble(stopWatch.ElapsedMilliseconds)/Convert.ToDouble(iterations);
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 26:     <span style="color: #0000ff">return</span> <span style="color: #0000ff">string</span>.Format(&quot;<span style="color: #8b0000">Return Cnt: {0} LINQ Executions: {1}      Milliseconds Per Iteration: {2}</span>&quot;, retCnt, iterations,
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 27:                          String.Format(&quot;<span style="color: #8b0000">{0:0.00}</span>&quot;, milliSecondsPerIteration));
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 28: }</pre>
</pre>



<p>And, the Compiled LINQ2SQL Code:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 90%; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ProcessLinqQueryCompiled()
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  2: {
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  3:     var stopWatch = <span style="color: #0000ff">new</span> Stopwatch();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  4:     stopWatch.Start();
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  5: 
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  6:     <span style="color: #0000ff">int</span> retCnt = 0;
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  7:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; iterations; i++)
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  8:     {
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  9:         <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> companyTypeIdToFind = 1;
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 10: 
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 11:         <span style="color: #0000ff">if</span> (meta == <span style="color: #0000ff">null</span>)
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 12:         {
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 13:             meta = <span style="color: #0000ff">new</span> DataClassesDataContext();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 14:         }
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 15: 
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 16:         <span style="color: #0000ff">if</span> (_compiledQuery == <span style="color: #0000ff">null</span>)
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 17:         {
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 18:             _compiledQuery =
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 19:                 CompiledQuery.Compile((DataClassesDataContext metax, <span style="color: #0000ff">int</span> myQuery) =&gt;
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 20:                                       (from dataCo <span style="color: #0000ff">in</span> metax.Companies
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 21:                                        where (from dataCt <span style="color: #0000ff">in</span> metax.CompanyCompanyTypes
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 22:                                               where dataCt.CompanyTypeId == companyTypeIdToFind
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 23:                                               select dataCt.CompanyId).Contains(dataCo.Id)
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 24:                                        select dataCo)
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 25:                     );
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 26:         }
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 27: 
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 28:         
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 29:         var query =
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 30:             (IOrderedQueryable&lt;Company&gt;)
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 31:             _compiledQuery(meta, 1);
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 32:         var newList = query.ToList();
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 33:         retCnt = newList.Count;
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 34:     }
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 35:     stopWatch.Stop();
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 36:     <span style="color: #0000ff">double</span> milliSecondsPerIteration = Convert.ToDouble(stopWatch.ElapsedMilliseconds) / Convert.ToDouble(iterations);
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 37:     <span style="color: #0000ff">return</span> <span style="color: #0000ff">string</span>.Format(&quot;<span style="color: #8b0000">Return Cnt: {0} LINQ Executions: {1}      Milliseconds Per Iteration: {2}</span>&quot;, retCnt, iterations,
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 38:                          String.Format(&quot;<span style="color: #8b0000">{0:0.00}</span>&quot;, milliSecondsPerIteration));
</pre>

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 39: 
</pre>

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> 40: }</pre>
</pre>



<p>Bottom line, You need to compile your LINQ2SQL!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/08/linq2sql-uncompiled-verses-compiled-iis-performance-aspnet/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Not Sure If I&#8217;ve Said Often Enough.. I LOVE LINQPAD!!!</title>
		<link>http://peterkellner.net/2009/06/04/yet-another-happy-linqpad-customer/</link>
		<comments>http://peterkellner.net/2009/06/04/yet-another-happy-linqpad-customer/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 13:30:51 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/04/yet-another-happy-linqpad-customer/</guid>
		<description><![CDATA[ I’ve been doing a lot of LINQ2SQL lately and just in case I have not said it loud enough how enthusiastic I am about LINQPAD written by Joseph Albahari.&#160; I have the $19 version which has intellisense, but even without, it would be totally awesome.   Today, I discovered you can actually put [...]]]></description>
			<content:encoded><![CDATA[<p> I’ve been doing a lot of LINQ2SQL lately and just in case I have not said it loud enough how enthusiastic I am about <a href="http://www.linqpad.net/">LINQPAD</a> written by Joseph Albahari.&#160; I have the $19 version which has intellisense, but even without, it would be totally awesome.</p> <span id="more-317"></span>  <p>Today, I discovered you can actually put c# right into the editor!&#160; I’m attaching a screen shot of how it helped me solve a simple LINQ command that involved an integer list.&#160; I reviewed it in a little more detail on a previous post here:&#160; <a title="http://peterkellner.net/2008/09/20/linqpad-sqlserver-linq-interpreter-nutshell/" href="http://peterkellner.net/2008/09/20/linqpad-sqlserver-linq-interpreter-nutshell/">http://peterkellner.net/2008/09/20/linqpad-sqlserver-linq-interpreter-nutshell/</a></p>  <p><a href="http://peterkellner.net/FilesForWebDownload/NotSureIfIveSaidOftenEnough.ILOVELINQPAD_5B93/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/NotSureIfIveSaidOftenEnough.ILOVELINQPAD_5B93/image_thumb.png" width="540" height="350" /></a></p>  <p>Hope you find it useful also!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/04/yet-another-happy-linqpad-customer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Fast are In Memory LINQ Evaluations for Doing Simple Things?</title>
		<link>http://peterkellner.net/2009/06/03/linq-in-memory-performance/</link>
		<comments>http://peterkellner.net/2009/06/03/linq-in-memory-performance/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 01:27:19 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/03/linq-in-memory-performance/</guid>
		<description><![CDATA[So, I recently blogged about the huge penalty for not compiling your LINQ2SQL.&#160; This problem is so big that it occurred to us that maybe all of LINQ has the problem.&#160; So, time for a simple test.&#160; Below is a very simple program that basically generates a list of Ids.&#160; In one case, it’s using [...]]]></description>
			<content:encoded><![CDATA[<p>So, I recently blogged about the huge penalty for <a href="http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/">not compiling your LINQ2SQL</a>.&#160; This problem is so big that it occurred to us that maybe all of LINQ has the problem.&#160; So, time for a simple test.&#160; Below is a very simple program that basically generates a list of Ids.&#160; In one case, it’s using LINQ, and the other just Plain C#.&#160; The code is pretty self explanatory.&#160; Here are the results:</p>  <table border="0" cellspacing="0" cellpadding="2" width="400"><tbody>     <tr>       <td valign="top" width="200">Test Performed</td>        <td valign="top" width="200">Time to Do 100000 iterations</td>     </tr>      <tr>       <td valign="top" width="200">Using LINQ</td>        <td valign="top" width="200">52ms</td>     </tr>      <tr>       <td valign="top" width="200">Using Simple C#</td>        <td valign="top" width="200">35ms</td>     </tr>   </tbody></table>  <p>Well, my fears are put aside.&#160; Though LINQ is somewhat slower, for 100,000 iterations, .052 seconds is pretty good.&#160; (compared with processing a single not to complicated LINQ2SQL statement for 100,000 iterations would take about 20,000 seconds or 333 hours.&#160; Quite a difference to .052 seconds!</p> <span id="more-316"></span>  <p>Here is the code I ran for my test.&#160; Hope this help!</p>  <div class="csharpcode"> <pre class="alt"><span class="kwrd">using</span> System;</pre>
<pre><span class="kwrd">using</span> System.Collections.Generic;</pre>
<pre class="alt"><span class="kwrd">using</span> System.Diagnostics;</pre>
<pre><span class="kwrd">using</span> System.Linq;</pre>
<pre class="alt"><span class="kwrd">using</span> System.Text;</pre>
<pre>&nbsp;</pre>
<pre class="alt"><span class="kwrd">namespace</span> LINQTimingInMemory</pre>
<pre>{</pre>
<pre class="alt">     <span class="kwrd">class</span> Program</pre>
<pre>     {</pre>
<pre class="alt">         <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)</pre>
<pre>         {</pre>
<pre class="alt">             <span class="kwrd">for</span> (<span class="kwrd">int</span> i=0;i&lt;2;i++)</pre>
<pre>             {</pre>
<pre class="alt">                 List&lt;<span class="kwrd">int</span>&gt; myList = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;() {1,2,3,4,5,6,7,8,9,6,5,5,5,5,5,5};</pre>
<pre>                 Stopwatch stopWatch = <span class="kwrd">new</span> Stopwatch();</pre>
<pre class="alt">                 stopWatch.Start();</pre>
<pre>                 <span class="kwrd">int</span> iterations = 100000;</pre>
<pre class="alt">                 <span class="kwrd">for</span> (<span class="kwrd">int</span> t = 0; t &lt; iterations; t++)</pre>
<pre>                 {</pre>
<pre class="alt">                     <span class="kwrd">if</span> (i == 0)</pre>
<pre>                     {</pre>
<pre class="alt">                         var ids = myList.Where(a =&gt; a &gt; 5).ToList();</pre>
<pre>                     }</pre>
<pre class="alt">                     <span class="kwrd">else</span></pre>
<pre>                     {</pre>
<pre class="alt">                         var ids = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;();</pre>
<pre>                         <span class="kwrd">foreach</span> (var v <span class="kwrd">in</span> myList)</pre>
<pre class="alt">                         {</pre>
<pre>                             <span class="kwrd">if</span> (v &gt; 5)</pre>
<pre class="alt">                             {</pre>
<pre>                                 ids.Add(v);</pre>
<pre class="alt">                             }</pre>
<pre>                         }</pre>
<pre class="alt">                     }</pre>
<pre>                 }</pre>
<pre class="alt">                 stopWatch.Stop();</pre>
<pre>                 <span class="kwrd">int</span> ti = Convert.ToInt32(stopWatch.ElapsedMilliseconds);</pre>
<pre class="alt">                 Console.WriteLine(<span class="str">"time for "</span> + i.ToString() + <span class="str">" "</span> + ti/iterations + <span class="str">" "</span> + ti);</pre>
<pre>                 stopWatch.Reset();</pre>
<pre class="alt">             }</pre>
<pre>         }</pre>
<pre class="alt">     }</pre>
<pre> }</pre>
</div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/03/linq-in-memory-performance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Create a Generic Integer List From a List of Objects Using LINQ</title>
		<link>http://peterkellner.net/2009/06/03/create-generic-list-integer-from-object-list/</link>
		<comments>http://peterkellner.net/2009/06/03/create-generic-list-integer-from-object-list/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 17:02:13 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/06/03/create-generic-list-integer-from-object-list/</guid>
		<description><![CDATA[So, this is very straight forward, but I sometimes forget it.&#160; I figure I’ll do a short blog post on it so next time I search for it, I’ll probably hit my own blog post.  So, say you have a list of objects as follows:   public partial class CompanyAddressResult : ResultBase
  [...]]]></description>
			<content:encoded><![CDATA[<p>So, this is very straight forward, but I sometimes forget it.&#160; I figure I’ll do a short blog post on it so next time I search for it, I’ll probably hit my own blog post.</p>  <p>So, say you have a list of objects as follows:</p> <span id="more-315"></span>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> CompanyAddressResult : ResultBase
    {
        <span class="kwrd">public</span> <span class="kwrd">int</span> Id {get;set;}
        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">int</span> CompanyId { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">int</span> AddressId { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">int</span> CompanyAddressTypeId { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">string</span> Notes { get; set; }
        <span class="kwrd">public</span> <span class="kwrd">int</span>? ActiveFlag { get; set; }
}</pre>

<pre class="csharpcode">And, you want to create a list of AddressIds.  Here is all you have to do:</pre>

<pre class="csharpcode">List&lt;<span class="kwrd">int</span>&gt; AddressIds = retList.Select(a =&gt; a.AddressId).ToList();</pre>

<pre class="csharpcode">Hope this helps.</pre>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/06/03/create-generic-list-integer-from-object-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQ To SQL Very Slow Performance Without Compile (CompileQuery)</title>
		<link>http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/</link>
		<comments>http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/#comments</comments>
		<pubDate>Wed, 06 May 2009 17:23:25 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/</guid>
		<description><![CDATA[<br/>
In this article, it is shown how important it can be to use the CompileQuery class when using LINQ to SQL.  Doing a simple query in LINQ to SQL verses ADO.NET is shown to run 37 times slower without taking advantage of the Compile feature in LINQ to SQL.  If you are doing the same query over and over in LINQ to SQL, this is a no brainer.  I'm surprised I did not know this myself previously.

<br/><br/>
<img src='http://peterkellner.net/FilesForWebDownload/LINQToSQLVerySlowPerformanceWithoutCompi_88A7/image.png' alt='Performance of LINQ to SQL compiled verses not and ADO.NET' />]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>  <p>At my company, we have been running a workflow process that is taking 6 hours to complete.&#160; This is a new project so we’ve had the good fortune to be able to use the latest Microsoft technologies.&#160; We are a Microsoft shop, so that means <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">Sql Server 2008</a>, <a href="http://msdn.microsoft.com/en-us/vcsharp/default.aspx">C#,</a> <a href="http://www.asp.net/">ASP.NET</a>, <a href="http://extjs.com/">ExtJS</a>, LINQ to SQL (hoping to move to Entity Framework someday) and others.&#160; After spending all day running performance analysis tools and basically performing a full audit of the 6 hour process, we sadly concluded that our time was being eaten up by <a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx">LINQ to SQL</a> processing.&#160; My experience has always been that anything you do on the compiled code side is usually overwhelmed by database access times, usually at least ten to one.&#160; Well, I learned a lot yesterday.</p> <span id="more-302"></span>  <h2>The Problem</h2>  <p>Since I already know the answer, I put together a very trivial problem to solve.&#160; What I want to do is execute one simple SQL statement over and over.&#160;&#160; I’m going to query just one table that actually has no records in it just to make sure the database really has nothing to do.&#160; I’m going to make this connection on my blazingly fast <a href="http://shop.lenovo.com/us/notebooks/thinkpad/w-series">Lenovo W500</a> with a SATA <a href="http://en.wikipedia.org/wiki/Solid-state_drive">SSD</a> running Sql Server 2008&#160; locally.&#160; I’m going to run that simple query first calling just ADO.NET (bare bones using best practices), Then using LINQ to SQL in the way I’ve always used it (and I would say 99% of the other developers out there), then I’m going to compile that LINQ to SQL and run it again.&#160; Actually, I’m going to iterate 2000 times for each of the three conditions.</p>  <p>So, here is the simple SQL I’m executing:</p>  <pre class="csharpcode"><span class="kwrd">SELECT</span> 
  Id,
  FileName,
  FileSize,
  UploadDate,
  DownloadDate,
  ProcessDate
<span class="kwrd">FROM</span>
  LinqTest
<span class="kwrd">WHERE</span> 
  dbo.LinqTest.FileName = <span class="str">'MyVal'</span></pre>

<h2>The Shocking Results</h2>

<p><a href="http://peterkellner.net/FilesForWebDownload/LINQToSQLVerySlowPerformanceWithoutCompi_88A7/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/LINQToSQLVerySlowPerformanceWithoutCompi_88A7/image_thumb.png" width="522" height="317" /></a></p>

<p>Or, for those graphically challenged (like search engines), Here is the actual data.</p>

<table border="0" cellspacing="0" cellpadding="0" width="332"><tbody>
    <tr>
      <td width="177">Test Description</td>

      <td width="153">Seconds Execution for 2000 Iterations</td>
    </tr>

    <tr>
      <td width="177">ADO.NET</td>

      <td width="153">0.48</td>
    </tr>

    <tr>
      <td width="177">Linq2Sql Not Compiled</td>

      <td width="153">18.14</td>
    </tr>

    <tr>
      <td width="177">Linq2Sql Compiled</td>

      <td width="153">0.87</td>
    </tr>
  </tbody></table>

<p>In English, this says that</p>

<ul>
  <li>LINQ to SQL is&#160; <strong>37 times slower</strong> than running a raw ADO.NET Sql query </li>

  <li>LINQ to SQL compiled <strong>1.8 times slower</strong> than running a raw ADO.NET Sql query </li>
</ul>

<p>I’ve read in several places that compiling your LINQ will help, but I have never heard anyone say how <u>drastic</u> the speed improvement can be.&#160; For example, in one of my favorite books (<a href="http://www.amazon.com/exec/obidos/ASIN/1933988169/petkelsblo-20">LINQ in Action</a>) by Fabrice Marguerie and others, he quotes on page 296&#160; a blog post by Rico Mariani titled <a href="http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx">DLINQ (Linq to SQL Performance (Part 1)</a> as saying using a compiled query offers nearly twice the performanced of a non-compiled query, and goes on to say that it brings the performance to within 93% of using a raw data reader.&#160;&#160; Well, suffice it to say I never ran the test myself.&#160; I could have lived with twice, but not 37 times.</p>

<p>Here is the Project File Source: </p>

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:5092a85c-00d3-4251-820d-af7837a27d56" class="wlWriterSmartContent">
  <p><a href="http://peterkellner.net/FilesForWebDownload/LINQToSQLVerySlowPerformanceWithoutCompi_88A7/LINQ2SQL_CompiledQuery.zip">Project Source (Visual Studio 2008 Solution)</a></p>
</div>

<h2>How to Compile you SQL (seems like a duh kind of thing)</h2>

<p>It’s actually not very hard.&#160; I’m attaching the Visual Studio 2008 project that I ran this test with to this post so you can run it for yourself as well as see an example of how to write the compiled LINQ code.&#160; Below is the method that actually does the work.&#160; I won’t include in this article the actual ADO.NET and LINQ to SQL not compiled code, but you can see those for yourself in the attached solution.</p>

<div class="csharpcode">
  <pre>&#160;</pre>

  <div class="csharpcode">
    <pre class="alt"><span class="lnum">   1:  </span><span class="rem">/// &lt;summary&gt;</span></pre>

    <pre><span class="lnum">   2:  </span><span class="rem">/// This method compiles the LINQ to SQL query and </span></pre>

    <pre class="alt"><span class="lnum">   3:  </span><span class="rem">/// then executes it the number of iterations passed</span></pre>

    <pre><span class="lnum">   4:  </span><span class="rem">/// in.  </span></pre>

    <pre class="alt"><span class="lnum">   5:  </span><span class="rem">/// &lt;/summary&gt;</span></pre>

    <pre><span class="lnum">   6:  </span><span class="rem">/// &lt;param name=&quot;iterations&quot;&gt;Number of iterations&lt;/param&gt;</span></pre>

    <pre class="alt"><span class="lnum">   7:  </span><span class="rem">/// &lt;returns&gt;time in seconds of execution&lt;/returns&gt;</span></pre>

    <pre><span class="lnum">   8:  </span><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">double</span> </pre>

    <pre class="alt"><span class="lnum">   9:  </span>    TestDataAccessSpeedLinq2SqlCompiled(<span class="kwrd">int</span> iterations)</pre>

    <pre><span class="lnum">  10:  </span>{</pre>

    <pre class="alt"><span class="lnum">  11:  </span>    Func&lt;DataClassesDataContext, <span class="kwrd">string</span>, IQueryable&lt;LinqTest&gt;&gt; compiledQuery =</pre>

    <pre><span class="lnum">  12:  </span>        CompiledQuery.Compile((DataClassesDataContext meta,<span class="kwrd">string</span> fileNameForSearch) =&gt;</pre>

    <pre class="alt"><span class="lnum">  13:  </span>                              (from myData <span class="kwrd">in</span> meta.LinqTests</pre>

    <pre><span class="lnum">  14:  </span>                               orderby myData.Id</pre>

    <pre class="alt"><span class="lnum">  15:  </span>                               <span class="kwrd">where</span> myData.FileName.Equals(fileNameForSearch)</pre>

    <pre><span class="lnum">  16:  </span>                               select myData));</pre>

    <pre class="alt"><span class="lnum">  17:  </span>&#160;</pre>

    <pre><span class="lnum">  18:  </span>    var metaNew = <span class="kwrd">new</span> DataClassesDataContext();</pre>

    <pre class="alt"><span class="lnum">  19:  </span>    DateTime startTime = DateTime.Now;</pre>

    <pre><span class="lnum">  20:  </span>    <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; iterations; i++)</pre>

    <pre class="alt"><span class="lnum">  21:  </span>    {</pre>

    <pre><span class="lnum">  22:  </span>        IOrderedQueryable&lt;LinqTest&gt; query = </pre>

    <pre class="alt"><span class="lnum">  23:  </span>            (IOrderedQueryable&lt;LinqTest&gt;) </pre>

    <pre><span class="lnum">  24:  </span>            compiledQuery(metaNew,<span class="kwrd">string</span>.Format(<span class="str">&quot;abcde{0}&quot;</span>, i));</pre>

    <pre class="alt"><span class="lnum">  25:  </span>        List&lt;LinqTest&gt; newList = query.ToList();</pre>

    <pre><span class="lnum">  26:  </span>    }</pre>

    <pre class="alt"><span class="lnum">  27:  </span>    <span class="kwrd">return</span> </pre>

    <pre><span class="lnum">  28:  </span>        DateTime.Now.Subtract(startTime).Duration().TotalSeconds;</pre>

    <pre class="alt"><span class="lnum">  29:  </span>}</pre>
  </div>
  <style type="text/css">



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style></div>
<style type="text/css">



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Essentially, line 11 is compiling the code into an instance variable called compileQuery.&#160; Using this instance variable, you can now execute the&#160; LINQ query in a compiled form while still passing in variable data such as fileNameToSearch.&#160; Again, the important thing to note is that one line 24 compiledQuery is already compiled so the IOrderedQueryable result is obtained without having to recompile the LINQ statement.</p>

<h2>Conclusions</h2>

<p>So, from this, it seems that you should always compile your LINQ to SQL queries.&#160; Well, that’s not quite true.&#160; What I’m recommending is that if you have a reason to execute the same query over and over you should strongly consider compiling.&#160; If for example, you are just making a LINQ to SQL call once, there is no benefit because you have to compile it anyway.&#160; Call it ten times?&#160; Well, you will have to decide for yourself.</p>

<p>Forewarned is forearmed!&#160; good luck and hope this helps.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>ASP.NET 3.5 Social Networking by Andrew Siemer (Book Review)</title>
		<link>http://peterkellner.net/2009/03/25/aspnet35-book-review-andrew-siemer-social-networking/</link>
		<comments>http://peterkellner.net/2009/03/25/aspnet35-book-review-andrew-siemer-social-networking/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:04:28 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[Book Reviews]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/03/25/aspnet35-book-review-andrew-siemer-social-networking/</guid>
		<description><![CDATA[Andrew Siemer has written an excellent book about how to best use .Net (specifically with asp.net as the front end web technology) to build a social networking site.&#160; He literally starts at the beginning and builds service by service, screen by screen, data table by data table what is necessary to build the site.&#160; A [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.andrewsiemer.com/">Andrew Siemer</a> has written an excellent book about how to best use .Net (specifically with asp.net as the front end web technology) to build a social networking site.&#160; He literally starts at the beginning and builds service by service, screen by screen, data table by data table what is necessary to build the site.&#160; A lot of Andrew shows is not only just web type stuff, but he shows many best practices for building .Net applications in general.</p>  <p>Among other things, the goes in to great detail on the following topics:</p> <span id="more-274"></span> <ul>   <li><a href="http://en.wikipedia.org/wiki/Domain-driven_design">Domain Driven Design</a> </li>    <li>Model View Presenter Pattern </li>    <li>Factory Pattern Using <a href="http://structuremap.sourceforge.net/Default.htm">Structure Map</a> (IOC) </li>    <li>Error Handling </li>    <li>Social Network Schema Designs (Friends and Groups) </li>    <li>Messaging </li>    <li>Data Layers (<a href="http://msdn.microsoft.com/en-us/library/bb386976.aspx">LINQ to SQL</a>) </li>    <li>Blog Design </li>    <li>Forums and Moderation </li> </ul>  <p>As well as many other topics including application scaling and many others.&#160; As developers, we have many choices as to what technologies to use and how to use them.&#160; I have to say, I agree with 95% of the choices Andrew makes and whole heartedly recommend reading his book.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:7dc1bd33-94bd-46fd-a20b-0131235bcd47:bb99355d-e17a-48bd-ad01-9277b1bfdd33" class="wlWriterSmartContent">   <table border="0" cellspacing="0" cellpadding="2" width="400"><tbody>       <tr>         <td valign="top" width="400">           <p><a title="ASP.NET 3.5 Social Networking: Andrew Siemer: Books" href="http://www.packtpub.com/expert-guide-for-social-networking-with-asp-.net-3.5/book/mid/260309n1518x"><img style="float: left" border="0" align="left" src="http://images.amazon.com/images/P/1847194788.01.MZZZZZZZ.jpg" />ASP.NET 3.5 Social Networking: Andrew Siemer: Books</a></p>         </td>       </tr>     </tbody></table> </div>  <p><a type="amzn" search="search">asp.net 3.5 social networking</a></p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/03/25/aspnet35-book-review-andrew-siemer-social-networking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Logging your Sql with LINQ Update Commands. Simple Logging to your Visual Studio 2008 Debugger Output Console</title>
		<link>http://peterkellner.net/2008/12/04/linq-debug-output-vs2008/</link>
		<comments>http://peterkellner.net/2008/12/04/linq-debug-output-vs2008/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 02:13:34 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2008/12/04/linq-debug-output-vs2008/</guid>
		<description><![CDATA[ So, you want to do an update but are wondering what the hec LINQ is doing.&#160; Turns out it is really easy.&#160; All you have to do is run in the debugger and add the Log option to your data context.  Here is an example:     DataClassesGeneralDataContext db3pLogicContext;

  db3pLogicContext.Log [...]]]></description>
			<content:encoded><![CDATA[<p> So, you want to do an update but are wondering what the hec LINQ is doing.&#160; Turns out it is really easy.&#160; All you have to do is run in the debugger and add the Log option to your data context.</p>  <p>Here is an example:</p>  <div class="csharpcode">   <pre class="alt">DataClassesGeneralDataContext db3pLogicContext;</pre>

  <pre>db3pLogicContext.Log = Console.Out;</pre>

  <pre class="alt">&#160;</pre>

  <pre>var companyQuery = from tbl <span class="kwrd">in</span> db3pLogicContext.Companies</pre>

  <pre class="alt">                   <span class="kwrd">where</span> tbl.ParentId != 0</pre>

  <pre>                   select tbl;</pre>

  <pre class="alt">&#160;</pre>

  <pre><span class="kwrd">int</span> totalCntParents = companyQuery.Count();</pre>

  <pre class="alt"><span class="kwrd">foreach</span> (DBAccess.Company co <span class="kwrd">in</span> companyQuery)</pre>

  <pre>{</pre>

  <pre class="alt">    co.CreateDate = DateTime.Now;</pre>

  <pre>    <span class="kwrd">break</span>;</pre>

  <pre class="alt">}</pre>

  <pre>&#160;</pre>

  <pre class="alt">db3pLogicContext.SubmitChanges();</pre>
</div>

<div class="csharpcode">&#160;</div>
<span id="more-192"></span>

<p>Now, when you run the code, you can look on your debugger output and see something that looks like this:</p>

<div class="csharpcode"><a href="http://peterkellner.net/blogimages/LoggingyourSqlwithLINQUpdateCommands_FEF2/image.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://peterkellner.net/blogimages/LoggingyourSqlwithLINQUpdateCommands_FEF2/image_thumb.png" width="431" height="417" /></a> </div>

<div class="csharpcode">&#160;</div>
<style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Also, if you want to actually execute select type statements, take a look at the Query Visualizer posted on Scott Guthrie&#8217;s web site:</p>

<p><a title="http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx" href="http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx">http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx</a></p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2008/12/04/linq-debug-output-vs2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 5/13 queries in 0.004 seconds using disk

Served from: peterkellner.net @ 2012-02-08 20:06:21 -->
