<?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 to SQL</title>
	<atom:link href="http://peterkellner.net/category/linq-to-sql/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>Is There A Need For End-To-End ExtJS to Microsoft Server (MVC-C#, LOB) 4 Day Class? (Poll Enclosed)</title>
		<link>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/</link>
		<comments>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/#comments</comments>
		<pubDate>Wed, 05 May 2010 02:54:13 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[OData]]></category>
		<category><![CDATA[Training]]></category>

		<guid isPermaLink="false">http://peterkellner.net/?p=1289</guid>
		<description><![CDATA[Over the past couple years, the focus of the web development I’ve been doing involves building highly flexible, highly scalable and straight forward web sites to implement and maintain Line of Business (LOB) applications.&#160; As you can probably tell from my posts, I’m very “practical” focused, and at the same time have a desire to [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past couple years, the focus of the web development I’ve been doing involves building highly flexible, highly scalable and straight forward web sites to implement and maintain <a href="http://en.wikipedia.org/wiki/Line_of_business">Line of Business</a> (LOB) applications.&#160; As you can probably tell from my posts, I’m very “practical” focused, and at the same time have a desire to build awesome web applications.</p>  <p>The technology pairing I’ve chosen is <a href="http://www.asp.net/mvc/whatisaspmvc/">Microsoft’s .Net platform with MVC</a> on the server, and <a href="http://www.extjs.com/">ExtJS</a> on the client.&#160; Though it’s possible to still use ExtJS with standard html/aspx pages, I’ve found the best combination is to use 100% <a href="http://www.javascript.com/">JavaScript</a> on the client (ExtJS) and have all the server side technology be 100% service based.&#160; I’ve used <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ2SQL</a> extensively as well as <a href="http://msdn.microsoft.com/en-us/magazine/ee336128.aspx">Entity Framework</a> in the latest <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a> release.</p>  <p>The learning curve was quite steep to actually be able to efficiently build highly flexible, highly scalable applications using these technologies, but now that I know it, I wouldn’t have it any other way.</p>  <p>I’m considering putting together a series of <strong>4 Day Classes</strong> around the country (or even world) that would basically teach people the methods and patterns I’ve learned and essentially leap frog a development team into being able to quickly do what it has taken me years to figure out.&#160; I’ve been fortunate enough to know the top 1% instructors and I’m sure with the right incentive, can get them to join me in both putting together these classes as well as teaching them.</p> <span id="more-1289"></span>  <p>So, my question is:</p> Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.   <p>I really appreciate your feedback.&#160; If I go down this path, it will be a lot of work and it would be nice to know if people are really interested.&#160; I know for myself, I&#8217; would have gone to something like this 2 years ago and it would have saved me a lot of time.</p>  <p>Thanks for your feedback.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>12</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>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>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>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>
	</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 2/11 queries in 0.002 seconds using disk

Served from: peterkellner.net @ 2012-02-10 04:24:12 -->
