<?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"
	>

<channel>
	<title>The .NET Business of Software in Thailand</title>
	<atom:link href="http://stephenedwards.virtualituk.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenedwards.virtualituk.com</link>
	<description>In theory there is no difference between theory and practice, but in practice there is.</description>
	<pubDate>Thu, 15 Jan 2009 13:59:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>JavaScript - links</title>
		<link>http://stephenedwards.virtualituk.com/2009/01/javascript-links/</link>
		<comments>http://stephenedwards.virtualituk.com/2009/01/javascript-links/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 10:44:37 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=36</guid>
		<description><![CDATA[Some JavaScript links
 
http://www.javascriptkit.com/javatutors/closures.shtml
 
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Class-Based_vs._Prototype-Based_Languages
]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 10pt; font-family: Arial;">Some JavaScript links</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 10pt; font-family: Arial;"><a href="http://www.javascriptkit.com/javatutors/closures.shtml"><span style="color: #800080;">http://www.javascriptkit.com/javatutors/closures.shtml</span></a></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 10pt; font-family: Arial;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 10pt; font-family: Arial;"><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Class-Based_vs._Prototype-Based_Languages"><span style="color: #800080;">https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Class-Based_vs._Prototype-Based_Languages</span></a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2009/01/javascript-links/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Closures - a none academic explanation that normal programmers can understand!</title>
		<link>http://stephenedwards.virtualituk.com/2009/01/closures-a-none-academic-explanation-that-normal-programmers-can-understand/</link>
		<comments>http://stephenedwards.virtualituk.com/2009/01/closures-a-none-academic-explanation-that-normal-programmers-can-understand/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 10:27:45 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Add new tag]]></category>

		<category><![CDATA[Closures]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=35</guid>
		<description><![CDATA[See this article which I have repeated below:
http://www.javascriptkit.com/javatutors/closures.shtml
It refers to closures in JavaScript but the explanation applies equally to C# delegates.
A closure is the local variables for a function - kept alive after the function has returned, or
A closure is a stack-frame which is not deallocated when the function returns. (as if a &#8217;stack-frame&#8217; were malloc&#8217;ed instead [...]]]></description>
			<content:encoded><![CDATA[<p>See this article which I have repeated below:</p>
<p><a href="http://www.javascriptkit.com/javatutors/closures.shtml">http://www.javascriptkit.com/javatutors/closures.shtml</a></p>
<p>It refers to closures in JavaScript but the explanation applies equally to C# delegates.</p>
<p>A closure is the local variables for a function - kept alive <em>after</em> the function has returned, or</p>
<p>A closure is a stack-frame which is <em>not deallocated</em> when the function returns. (as if a &#8217;stack-frame&#8217; were malloc&#8217;ed instead of being on the stack!)</p>
<p>Look at the article for the fuller explaination.</p>
<p><a href="http://www.javascriptkit.com/javatutors/closures.shtml">http://www.javascriptkit.com/javatutors/closures.shtml</a></p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2009/01/closures-a-none-academic-explanation-that-normal-programmers-can-understand/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Location Of IE Favorites</title>
		<link>http://stephenedwards.virtualituk.com/2009/01/location-of-ie-favorites/</link>
		<comments>http://stephenedwards.virtualituk.com/2009/01/location-of-ie-favorites/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 09:54:35 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[IE]]></category>

		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=34</guid>
		<description><![CDATA[To changed the location of my IE favorites from my local hard disk to a network share:
1. Start Regedit
2. Go to HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
3. Double click on Favorites in the right hand panel
4. Enter the new location
5. Click on the OK button
6. Do the same for the User Shell Folders section a few lines down
7. Close [...]]]></description>
			<content:encoded><![CDATA[<p>To changed the location of my IE favorites from my local hard disk to a network share:</p>
<p>1. Start Regedit<br />
2. Go to HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders<br />
3. Double click on Favorites in the right hand panel<br />
4. Enter the new location<br />
5. Click on the OK button<br />
6. Do the same for the User Shell Folders section a few lines down<br />
7. Close Regedit<br />
8. Create the directory you just pointed to<br />
9. Copy any existing Favorites to that location<br />
10. Be sure to include all folders<br />
11. Start the Internet Explorer</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2009/01/location-of-ie-favorites/feed/</wfw:commentRss>
		</item>
		<item>
		<title>.NET Code Access Security - Network Drives</title>
		<link>http://stephenedwards.virtualituk.com/2008/11/net-code-access-security-network-drives/</link>
		<comments>http://stephenedwards.virtualituk.com/2008/11/net-code-access-security-network-drives/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 11:01:03 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[.NET Security]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=33</guid>
		<description><![CDATA[I&#8217;m saving this so I can find it easily later. Chris Sells has a post that describes how to give a Mapped Network Drive &#8220;FullTrust.&#8221; Thanks Chris!
[When loading a Project from a Network Drive (like Z:\, perhaps within a VPC) you may receive:]
The project location is not trusted.
Running the application may result in security exceptions when [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m saving this so I can find it easily later. Chris Sells has <a href="http://www.sellsbrothers.com/news/showTopic.aspx?ixTopic=1519">a post that describes how to give a Mapped Network Drive &#8220;FullTrust.&#8221;</a> Thanks Chris!</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p><em>[When loading a Project from a Network Drive (like Z:\, perhaps within a VPC) you may receive:]</em></p>
<blockquote style="margin-right: 0px;" dir="ltr"><p><em>The project location is not trusted.<br />
Running the application may result in security exceptions when it<br />
attempts to perform actions which require full trust.</em></p></blockquote>
<p dir="ltr"><em>What&#8217;s happening is that VS is detecting that the project on the network drive is getting Intranet permissions according to the good and true workings of .NET Code Access Security (CAS). However, since I&#8217;m just trying to pretend that Z is on my PC (and, in fact, it is), I want it to have FullTrust permissions. </em></p>
<p dir="ltr"><em>To accomplish this, you need to add a new Code Group with an URL membership permission specifying the folder (in URL form) to which you&#8217;d like to grant full trust. You can do with the .NET Framework Configuration tool or you can do it from the command line like so:</em></p>
<p><em><span style="font-family: Courier New;"><strong>c:\&gt;caspol -q -machine -addgroup 1 -url </strong></span></em><a href="file:///z:/"><em><span style="font-family: Courier New;"><strong>file://z:/</strong></span></em></a><em><span style="font-family: Courier New;"><strong>* FullTrust -name &#8220;Z Drive&#8221;</strong><br />
</span></em></p>
<p><em><span style="font-family: Courier New;">OR</span></em></p>
<p><span style="font-family: Courier New;">CasPol.exe -pp off -m -ag 1.2 -url file://\\ShawnFa-Srv/Tools/* FullTrust<br />
copy config\security.config config\security.config.default</span></p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr">
<div><em><br />
Once this new code group is in place, any new .NET processes you start will give any assemblies on the Z drive full trust.</em></div>
<p><em><em>Since awarding new permissions, full trust or not, to any chunk of code is something that can cause a security hole, be careful. </em></p>
<p> </p>
<p></em></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2008/11/net-code-access-security-network-drives/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dynamic LINQ to SQL</title>
		<link>http://stephenedwards.virtualituk.com/2008/07/dynamic-linq-to-sql/</link>
		<comments>http://stephenedwards.virtualituk.com/2008/07/dynamic-linq-to-sql/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 18:04:28 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Linq]]></category>

		<category><![CDATA[Add new tag]]></category>

		<category><![CDATA[Dynamic]]></category>

		<category><![CDATA[LINQ to SQL]]></category>

		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=27</guid>
		<description><![CDATA[ I came across the following statement about LINQ to SQL on a developer forum the other day:
“I think the general consensus is this: it’s good if you know the query beforehand and what type of object you&#8217;re going to get back on execution, but if the query is dynamic and/or large then it’s not well [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: small; font-family: Calibri;"> </span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I came across the following statement about LINQ to SQL on a developer forum the other day:</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">“</span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I think the general consensus is this: it’s good if you know the query beforehand and what type of object you&#8217;re going to get back on execution, but if the query is dynamic and/or large then it’s not well suited.”</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I thought I would kick off this blog by challenging this statement with some simple examples:</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The contributor was partly referring to his belief that it was not possible to do anything similar to dynamic SQL using LINQ to SQL and in fact posted a challenge to do in LINQ to SQL the equivalent of the following dynamic SQL:</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">string dynamicSQL = &#8221; SELECT &#8221; + <span style="mso-spacerun: yes;"> </span>&lt;someCols&gt; <span style="mso-spacerun: yes;"> </span>+ &#8221; <span style="mso-spacerun: yes;"> </span>FROM &#8221; + &lt;someTable&gt; + &#8221; WHERE &#8221; + &lt;someCondition&gt; + &#8221; ORDERBY &#8221; + &lt;orderBy&gt;;</span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">So we will look at how this can be done using LINQ to SQL.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">Before looking at this it’s useful to observe that when you dynamically create the <span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">&lt;someCondition&gt; for the WHERE clause you would need to distinguish between types when constructing it dynamically. For example:</span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">WHERE Quantity = 3 AND City=’London’<span style="mso-spacerun: yes"><span style="mso-spacerun: yes;">  </span><span style="mso-spacerun: yes;"> </span></span>(Assuming Quantity is a number of some type)</span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">You need to know that Quantity is a number and City is a string in order to correctly construct the SQL WHERE clause. What I am getting at is that when constructing dynamic SQL you have to be somewhat aware of what types you are dealing with.</span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The first thing to remember is that when we construct a LINQ to SQL query, that query is in fact stored as an expression tree. LINQ to SQL uses this expression tree to generate SQL which is sent to the database. </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">So the starting point for doing <em style="mso-bidi-font-style: normal;">dynamic</em> LINQ to SQL is to look at the </span><strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">System.Linq.Expressions</span></strong><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> namespace. </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The </span><strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">System.Linq.Expressions</span></strong><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> namespace contains classes, interfaces and enumerations that enable language-level code expressions to be represented as objects in the form of expression trees. It enables us to directly manipulate expression trees at runtime. Manipulating expression trees at runtime allows us to create LINQ to SQL queries dynamically.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">However, creating LINQ to SQL expression trees directly is not very convenient and is a none trivial task. What would be nice is to have the ability to convert a string representation of a LINQ to SQL query that can be parsed into an expression tree. </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">As it happens, there is a Dynamic Query Library written in C# that does precisely this and it is available at:</span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000;"><a href="http://msdn2.microsoft.com/en-us/vcsharp/bb894665.aspx" target="_blank"><span style="font-family: ">C# Dynamic Query Library (included in the \LinqSamples\DynamicQuery directory)</span></a></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">(The samples also include the <strong style="mso-bidi-font-weight: normal;">NorthwindMapping</strong> project which contains the mapping classes for the Northwind database. The examples here are against this mapping.)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">If you look in this library (</span><strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">System</span></strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">.<strong>Linq</strong>.<strong>Dynamic</strong><span style="color: #000000;">) you will see that it makes extensive use of the </span><strong>System.Linq.Expressions</strong><span style="color: #000000;"> and </span><strong>System.Reflection</strong><span style="color: #000000;"> namespace to implement its functionality.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">Using this library we are able to construct a potentially fully dynamic LINQ to SQL query.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I will use as my example the LINQ query:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">var</span> query <strong><span style="color: #800000;">=</span></strong></span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">from</span> c <span style="color: #0000ff;">in</span> db<strong><span style="color: #800000;">.</span></strong>Customers</span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">where</span> c<strong><span style="color: #800000;">.</span></strong>City <strong><span style="color: #800000;">==</span></strong> <strong><span style="color: #a31515;">&#8220;London&#8221;</span></strong> <strong><span style="color: #800000;">&amp;&amp;</span></strong> c<strong><span style="color: #800000;">.</span></strong>Orders<strong><span style="color: #800000;">.</span></strong>Count <strong><span style="color: #800000;">&gt;=</span></strong> <strong><span style="color: #ff0000;">10</span></strong></span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">select</span> <span style="color: #0000ff;">new</span> { Name <strong><span style="color: #800000;">=</span></strong> c<strong><span style="color: #800000;">.</span></strong>CompanyName, Phone <strong><span style="color: #800000;">=</span></strong> c<strong><span style="color: #800000;">.</span></strong>Phone };</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">expressed here in the C# LINQ syntax. Or:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">var</span> query <strong><span style="color: #800000;">=</span></strong></span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                 </span>db<strong><span style="color: #800000;">.</span></strong>Customers</span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #800000;">.</span></strong>Where(c <strong><span style="color: #800000;">=&gt;</span></strong> c<strong><span style="color: #800000;">.</span></strong>City <strong><span style="color: #800000;">==</span></strong> <strong><span style="color: #a31515;">&#8220;London&#8221;</span></strong> <strong><span style="color: #800000;">&amp;&amp;</span></strong> c<strong><span style="color: #800000;">.</span></strong>Orders<strong><span style="color: #800000;">.</span></strong>Count <strong><span style="color: #800000;">&gt;=</span></strong> <strong><span style="color: #ff0000;">10</span></strong>)</span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #800000;">.</span></strong>OrderBy(c<strong><span style="color: #800000;">=&gt;</span></strong>c<strong><span style="color: #800000;">.</span></strong>CompanyName)</span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="mso-no-proof: yes;"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #800000;">.</span></strong>Select(c <strong><span style="color: #800000;">=&gt;</span></strong> <span style="color: #0000ff;">new</span> { Name <strong><span style="color: #800000;">=</span></strong> c<strong><span style="color: #800000;">.</span></strong>CompanyName, Phone <strong><span style="color: #800000;">=</span></strong> c<strong><span style="color: #800000;">.</span></strong>Phone });</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">expressed here using extension methods. These two queries equate to the same thing.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The </span><strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">System</span></strong><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">.<strong>Linq</strong>.<strong>Dynamic</strong><span style="color: #000000;"> namespace basically provides a number of overloads to the extension methods for the </span></span><span style="font-size: 8pt; color: #2b91af; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">IQueryable</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">&lt;</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">T<strong><span style="color: #800000;">&gt;</span></strong></span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> and </span><span style="font-size: 8pt; color: #2b91af; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">IQueryable </span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">interfaces that accept string representations of lambda expressions. For example the signatures for the <strong style="mso-bidi-font-weight: normal;">Where</strong> extension methods are:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #2b91af;">IQueryable</span><strong><span style="color: #800000;">&lt;</span></strong>T<strong><span style="color: #800000;">&gt;</span></strong> Where<strong><span style="color: #800000;">&lt;</span></strong>T<strong><span style="color: #800000;">&gt;</span></strong>(</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">this</span> <span style="color: #2b91af;">IQueryable</span><strong><span style="color: #800000;">&lt;</span></strong>T<strong><span style="color: #800000;">&gt;</span></strong> source, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">string</span> predicate, <span style="color: #0000ff;">params</span> <span style="color: #0000ff;">object</span>[] values) {}</span><span style="font-size: 9pt; color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">and </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #2b91af;">IQueryable</span> Where<strong><span style="color: #800000;">&lt;</span></strong>T<strong><span style="color: #800000;">&gt;</span></strong>(</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">this</span> <span style="color: #2b91af;">IQueryable</span> source, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">string</span> selector, <span style="color: #0000ff;">params</span> <span style="color: #0000ff;">object</span>[] values) {}</span><span style="font-size: 9pt; color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">Of course we must make these extensions available by including the using statement:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-size: 9pt; color: #0000ff; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">using</span><span style="font-size: 9pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> System<strong><span style="color: #800000;">.</span></strong>Linq<strong><span style="color: #800000;">.</span></strong>Dynamic;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">Using these overloaded extension methods we can re-write the above query using string representations like so:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: #0000ff; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">string</span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> connString <strong><span style="color: #800000;">=</span></strong> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: #a31515; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">@&#8221;Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: #a31515; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-tab-count: 4;">                                </span>Security=True&#8221;</span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Northwind</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> db <strong><span style="color: #800000;">=</span></strong> <span style="color: #0000ff;">new</span> <strong><span style="color: #23778f;">Northwind</span></strong>(connString);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Assembly</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> asm <strong><span style="color: #800000;">=</span></strong> <strong><span style="color: #23778f;">Assembly</span><span style="color: #800000;">.</span></strong>GetAssembly(<span style="color: #0000ff;">typeof</span>(<strong><span style="color: #23778f;">Northwind</span></strong>));</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: #0000ff; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">var</span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> q <strong><span style="color: #800000;">=</span></strong> db<strong><span style="color: #800000;">.</span></strong>GetTable(asm<strong><span style="color: #800000;">.</span></strong>GetType(<strong><span style="color: #a31515;">&#8220;NorthwindMapping.Customer&#8221;</span></strong>))</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt; line-height: normal; mso-layout-grid-align: none;"><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">.</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Where(<strong><span style="color: #a31515;">&#8220;City == @0 and Orders.Count &gt;= @1&#8243;</span></strong>, <strong><span style="color: #a31515;">&#8220;London&#8221;</span></strong>, <strong><span style="color: #ff0000;">10</span></strong>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt; line-height: normal; mso-layout-grid-align: none;"><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">.</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">OrderBy(<strong><span style="color: #a31515;">&#8220;CompanyName, City&#8221;</span></strong>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt; line-height: normal; mso-layout-grid-align: none;"><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">.</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Select(<strong><span style="color: #a31515;">&#8220;New(CompanyName as Name, Phone)&#8221;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: small; font-family: Calibri;"> </span></span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I have used the </span><span style="mso-no-proof: yes;">db<strong><span style="color: #800000;">.</span></strong>GetTable(asm<strong><span style="color: #800000;">.</span></strong>GetType(<strong><span style="color: #a31515;">&#8220;NorthwindMapping.Customer&#8221;</span></strong>)) </span></span></span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">statement to get the mapping object dynamically too, however the extension methods have taken care of the rest.</span></span></span></p>
<p> </p>
<p> </p>
<div></div>
<p><span style="font-size: 8pt; color: #0000ff; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: small; font-family: Calibri;">Everything is expressed as strings with the exception that the parameters for the <strong style="mso-bidi-font-weight: normal;">Where</strong> clause need to be the correct type, or at least of types that are implicitly convertible to the correct type. However as I mentioned before, you would need some type awareness when constructing an SQL <strong style="mso-bidi-font-weight: normal;">WHERE</strong> clause too. </span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">I could put this into a general purpose method:</span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #2b91af;">IQueryable</span> FullDynamic(</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>System<strong><span style="color: #800000;">.</span></strong>Data<strong><span style="color: #800000;">.</span></strong>Linq<strong><span style="color: #800000;">.</span><span style="color: #23778f;">DataContext</span></strong> dc,<span style="mso-spacerun: yes;">            </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">string</span> tableName, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">string</span> orderBy, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">string</span> select,</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">string</span> wherePredicate, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">params</span> <span style="color: #0000ff;">object</span>[] whereValues</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #2b91af;">IQueryable</span> q <strong><span style="color: #800000;">=</span></strong> dc<strong><span style="color: #800000;">.</span></strong>GetTable(</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #23778f;">Assembly</span><span style="color: #800000;">.</span></strong>GetAssembly(dc<strong><span style="color: #800000;">.</span></strong>GetType())<strong><span style="color: #800000;">.</span></strong>GetType(tableName))</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #800000;">.</span></strong>Where(wherePredicate, whereValues)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #800000;">.</span></strong>OrderBy(orderBy)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #800000;">.</span></strong>Select(select);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">return</span> q;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000;"><span style="font-size: small;"><span style="font-family: Calibri;"> </span></span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt"><span style="color: #000000;"><span style="font-size: small;"><span style="font-family: Calibri;">And call it thus:</span></span></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes"><span style="mso-spacerun: yes;">            </span><span style="color: #2b91af;">IQueryable</span> q <strong><span style="color: #800000;">=</span></strong> FullDynamic(</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>db, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;NorthwindMapping.Customer&#8221;</span></strong>, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;CompanyName, City&#8221;</span></strong>, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;New(CompanyName as Name, Phone)&#8221;</span></strong>, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;City == @0 and Orders.Count &gt;= @1&#8243;</span></strong>, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;London&#8221;</span></strong>, <strong><span style="color: #ff0000;">10</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">foreach</span> (System<strong><span style="color: #800000;">.</span></strong>Linq<strong><span style="color: #800000;">.</span></strong>Dynamic<strong><span style="color: #800000;">.</span><span style="color: #23778f;">DynamicClass</span></strong> item <span style="color: #0000ff;">in</span> q)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: #800000;">.</span></strong>WriteLine(item);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000;"><span style="font-size: small;"><span style="font-family: Calibri;">I would need to do a little more work on this to enable changing the direction of the order by, but I think that would be about all to answer the challenge fully.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt;"><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: 'Lucida Sans Unicode';"> </span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">As you can see the resulting items from the dynamic query inherit from the </span></span></span><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">System</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">Linq</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">Dynamic</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">DynamicClass</span></strong><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> abstract type. </span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">foreach</span> (System<strong><span style="color: #800000;">.</span></strong>Linq<strong><span style="color: #800000;">.</span></strong>Dynamic<strong><span style="color: #800000;">.</span><span style="color: #23778f;">DynamicClass</span></strong> item <span style="color: #0000ff;">in</span> query2)</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">PropertyInfo</span></strong>[] props <strong><span style="color: #800000;">=</span></strong> item<strong><span style="color: #800000;">.</span></strong>GetType()<strong><span style="color: #800000;">.</span></strong>GetProperties(<span style="color: #2b91af;">BindingFlags</span><strong><span style="color: #800000;">.</span></strong>Instance <strong><span style="color: #800000;">|</span></strong> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-tab-count: 4;">                                </span><span style="color: #2b91af;">BindingFlags</span><strong><span style="color: #800000;">.</span></strong>Public);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">StringBuilder</span></strong> sb <strong><span style="color: #800000;">=</span></strong> <span style="color: #0000ff;">new</span> <strong><span style="color: #23778f;">StringBuilder</span></strong>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>sb<strong><span style="color: #800000;">.</span></strong>Append(<strong><span style="color: #a31515;">&#8220;{&#8221;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i <strong><span style="color: #800000;">=</span></strong> <strong><span style="color: #ff0000;">0</span></strong>; i <strong><span style="color: #800000;">&lt;</span></strong> props<strong><span style="color: #800000;">.</span></strong>Length; i<strong><span style="color: #800000;">++</span></strong>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: #0000ff;">if</span> (i <strong><span style="color: #800000;">&gt;</span></strong> <strong><span style="color: #ff0000;">0</span></strong>) sb<strong><span style="color: #800000;">.</span></strong>Append(<strong><span style="color: #a31515;">&#8220;, &#8220;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>sb<strong><span style="color: #800000;">.</span></strong>Append(props[i]<strong><span style="color: #800000;">.</span></strong>Name);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>sb<strong><span style="color: #800000;">.</span></strong>Append(<strong><span style="color: #a31515;">&#8220;=&#8221;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>sb<strong><span style="color: #800000;">.</span></strong>Append(props[i]<strong><span style="color: #800000;">.</span></strong>GetValue(item, <span style="color: #0000ff;">null</span>));</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>sb<strong><span style="color: #800000;">.</span></strong>Append(<strong><span style="color: #a31515;">&#8220;}&#8221;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: #800000;">.</span></strong>WriteLine(sb<strong><span style="color: #800000;">.</span></strong>ToString());</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: small; font-family: Calibri;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: small; font-family: Calibri;">This demonstrates that you can use reflection to inspect the resulting object graph at runtime – nothing new here, just straight forward use of reflection. In fact this code is taken from the ToString() overload of the </span></span><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">System</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">Linq</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><span style="font-size: 8pt; color: #000000; font-family: Consolas; mso-no-proof: yes;">Dynamic</span><strong><span style="font-size: 8pt; color: #800000; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">.</span></strong><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">DynamicClass</span></strong><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"> abstract type.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: small; font-family: Calibri;">I hope this is helpful. In my next article I will look at the other issue raised, of large or complex queries in LINQ to SQL.</span></span><span style="color: #000000; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"></span></span></p>
<p> </p>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2008/07/dynamic-linq-to-sql/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ADO.NET Entity Framework - my &#8220;getting started notes&#8221; - Part One</title>
		<link>http://stephenedwards.virtualituk.com/2008/07/getting-started-notes/</link>
		<comments>http://stephenedwards.virtualituk.com/2008/07/getting-started-notes/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 06:16:40 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[ADO.NET Entity Framework]]></category>

		<category><![CDATA[Data Platform]]></category>

		<category><![CDATA[EDM]]></category>

		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=8</guid>
		<description><![CDATA[I am just getting started with the ADO.NET Entity Framework myself and I thought I&#8217;d share things about the experience as I go. Who knows, it might be of use to someone out there! Please, if you see something here that is not correct then let me know.
I found the following article a useful read [...]]]></description>
			<content:encoded><![CDATA[<p style="margin: 0cm 0cm 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-line-height-alt: 10.45pt;">I am just getting started with the ADO.NET Entity Framework myself and I thought I&#8217;d share things about the experience as I go. Who knows, it might be of use to someone out there! Please, if you see something here that is not correct then let me know.</p>
<p style="margin: 0cm 0cm 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-line-height-alt: 10.45pt;">I found the following article a useful read for starters, although some of the detail is a little out of date I think. It&#8217;s a good overview if you need one:</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-line-height-alt: 10.45pt;"><a href="http://msdn.microsoft.com/en-us/magazine/cc163399.aspx">http://msdn.microsoft.com/en-us/magazine/cc163399.aspx</a></p>
<p style="margin: 0cm 0cm 10pt;">Visual Studio has a visual designer to build entity models and it generates a single EDMX file which it will generate code from using the EntityModelCodeGenerator custom tool.</p>
<p style="margin: 0cm 0cm 10pt;">There is a project called “Sample EDMX Code Generator” (source code available):</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><a href="http://code.msdn.microsoft.com/sampleedmxcodegen">http://code.msdn.microsoft.com/sampleedmxcodegen</a></p>
<p style="margin: 0cm 0cm 10pt;">which can also generate code from EDMX files. The project overview says:</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">
<div style="margin: 0cm 0cm 10pt;">The goal of the Sample EDMX Code Generator is to provide you with enough insight into how the ADO.NET Entity Designer generates code in Visual Studio and hopefully give you a head start with some sample source code.</div>
<div>SampleEdmxCodeGenerator is not intended for production use; instead, it demonstrates custom tool extensibility via SingleFileGenerator, code generation APIs, code generation events and EDM metadata APIs. The sample is also somewhat incomplete on error handling and has not been tuned for performance, stress, etc.</div>
<p>The Entity Framework Samples</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><a href="http://code.msdn.microsoft.com/EFQuerySamples">http://code.msdn.microsoft.com/EFQuerySamples</a></p>
<p style="margin: 0cm 0cm 10pt;">project uses separate entity model files however:</p>
<p style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt; mso-list: l0 level1 lfo1;">·         Conceptual - CSDL</p>
<p style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt; mso-list: l0 level1 lfo1;">·         Mapping - MSL</p>
<p style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt; mso-list: l0 level1 lfo1;">·         Logical – SSDL</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt 36pt;"><a href="http://stephenedwards.virtualituk.com/wp-content/uploads/2008/07/entityframework4.jpg"><img class="alignnone size-medium wp-image-13" title="entityframework4" src="http://stephenedwards.virtualituk.com/wp-content/uploads/2008/07/entityframework4-192x300.jpg" alt="" width="192" height="300" /></a></p>
<p>In VS these don’t appear to have a designer and so one cannot see a visual representation of the model in VS.</p>
<p style="margin: 0cm 0cm 10pt;">It would appear that the EDMX file combines the CSDL, MSL and SSDL files into one file. The XML namespaces (xmlns) in the EDMX file sections are the same as those in the individual files. It also contains additional sections used by the VS designer.</p>
<p style="margin: 0cm 0cm 10pt;">The CSDL defines the entities and the relationships as the application’s business layer will know them. The <strong>EntityModelCodeGenerator </strong>custom tool will generate code, classes from the CSDL, that represent the domain model. </p>
<p style="margin: 0cm 0cm 10pt;">In VS the custom tool can be invoked manually from the context menu of the EDMX or CSDL file in solution explorer and or a separate CSDL file - select &#8220;Run Custom Tool&#8221; - it will normally be set to run the  <strong>EntityModelCodeGenerator </strong>custom tool, although you can of course change this.</p>
<p style="margin: 0cm 0cm 10pt;"><strong>SSDL</strong></p>
<p style="margin: 0cm 0cm 10pt;">The SSDL file or the SSDL section of the EDMX file defines the structure of the relational data in the database.</p>
<p style="margin: 0cm 0cm 10pt;"><strong>MSL</strong></p>
<p style="margin: 0cm 0cm 10pt;">The MSL file or the MSL section of the EDMX file contains the direct map from the CSDL to the SSDL.</p>
<p style="margin: 0cm 0cm 10pt;">The CSDL, SSDL and MSL are collectively called the EDM – Entity Data Model.</p>
<p style="margin: 0cm 0cm 10pt;">If you generate an EDM from a database and immediately open the CSDL and SSDL files without making modifications to them, you will find that they are similar because the models are generated directly from the database and the conceptual model is mapped directly to the logical store.</p>
<p style="margin: 0cm 0cm 10pt;">This, of course, does not demonstrate the capabilities of the EDM. At this point the EDM looks very similar to what we would have when creating entities and mappings for LINQ to SQL – just a one to one mapping. LINQ to SQL entity mapping is capable of doing a little more; it supports Table per Hierarchy (TPH), where multiple classes in a hierarchy are mapped to a single table using a discriminator column to determine the specific type of each row/instance (this is a very common database inheritance mapping). One can do this using EF also of course. However, we can see that the EDM allows us to separate the conceptual model from the database model and to connect the two using a mapping.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">
<p style="margin: 0cm 0cm 10pt;">The real power of the EDM from the OO programmers point of view, then, is dependent on its mapping capabilities. I will be looking at Just how close our conceptual model can be to our ideal OO design for a business domain and yet be usefully mapped to the data model with EF. To me that is the key to EF and it&#8217;s usefulness.</p>
<p style="margin: 0cm 0cm 10pt;">Clearly EF intends to go further than the current crop of OR mapping tools in that it is clearly aiming at bridging the object to relational storage impedance mismatch in a different way.</p>
<p style="margin: 0cm 0cm 10pt;">How well it does this remains to be seen&#8230;&#8230;&#8230;&#8230;!!</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2008/07/getting-started-notes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Composable LINQ to SQL</title>
		<link>http://stephenedwards.virtualituk.com/2008/07/composable-linq-to-sql/</link>
		<comments>http://stephenedwards.virtualituk.com/2008/07/composable-linq-to-sql/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 15:31:07 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Data Platform]]></category>

		<category><![CDATA[Linq]]></category>

		<category><![CDATA[Dynamic]]></category>

		<category><![CDATA[LINQ to SQL]]></category>

		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=7</guid>
		<description><![CDATA[


In LINQ to SQL, queries can be composed. We can use this ability to create dynamic queries. For example:
            Northwind db = new Northwind(connString);
 
            var query =
                from c in db.Customers
                where c.City == &#8220;London&#8221;
                select c;
 
            Console.WriteLine(db.GetCommand(query).CommandText);
 
            if (true)  // Some condition
            {
                query =
                    from c in query
                    where c.ContactTitle == &#8220;Sales Representative&#8221;
                    [...]]]></description>
			<content:encoded><![CDATA[<div></div>
<div><span style="font-size: xx-small;"></span></div>
<p><span style="font-size: xx-small;"><span style="font-size: small; font-family: Calibri;"></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 175%;"><span style="color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; letter-spacing: -0.55pt; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB;">In LINQ to SQL, queries can be composed. We can use this ability to create dynamic queries. For example:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">Northwind</span></strong> db <strong><span style="color: maroon;">=</span></strong> <span style="color: blue;">new</span> <strong><span style="color: #23778f;">Northwind</span></strong>(connString);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">var</span> query <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">from</span> c <span style="color: blue;">in</span> db<strong><span style="color: maroon;">.</span></strong>Customers</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">where</span> c<strong><span style="color: maroon;">.</span></strong>City <strong><span style="color: maroon;">==</span></strong> <strong><span style="color: #a31515;">&#8220;London&#8221;</span></strong><span style="color: #a31515;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">select</span> c;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(db<strong><span style="color: maroon;">.</span></strong>GetCommand(query)<strong><span style="color: maroon;">.</span></strong>CommandText);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">if</span> (<span style="color: blue;">true</span>)<span style="mso-spacerun: yes;">  </span><span style="color: green;">// Some condition</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>query <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">from</span> c <span style="color: blue;">in</span> query</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">where</span> c<strong><span style="color: maroon;">.</span></strong>ContactTitle <strong><span style="color: maroon;">==</span></strong> <strong><span style="color: #a31515;">&#8220;Sales Representative&#8221;</span></strong><span style="color: #a31515;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">select</span> c;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(db<strong><span style="color: maroon;">.</span></strong>GetCommand(query)<strong><span style="color: maroon;">.</span></strong>CommandText);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">/*</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>SELECT </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[CustomerID], [t0].[CompanyName], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[ContactName], [t0].[ContactTitle], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[Address], [t0].[City], [t0].[Region], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[PostalCode], [t0].[Country], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[Phone], [t0].[Fax]</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>FROM [dbo].[Customers] AS [t0]</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>WHERE ([t0].[ContactTitle] = @p0) AND ([t0].[City] = @p1) </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>*/</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">if</span> (<span style="color: blue;">true</span>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>query <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>(</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">from</span> c <span style="color: blue;">in</span> query</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">from</span> o <span style="color: blue;">in</span> c<strong><span style="color: maroon;">.</span></strong>Orders</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">where</span> o<strong><span style="color: maroon;">.</span></strong>ShipCity <strong><span style="color: maroon;">==</span></strong> <strong><span style="color: #a31515;">&#8220;Colchester&#8221;</span></strong><span style="color: #a31515;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: blue;">select</span> c</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>)<strong><span style="color: maroon;">.</span></strong>Distinct();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(db<strong><span style="color: maroon;">.</span></strong>GetCommand(query)<strong><span style="color: maroon;">.</span></strong>CommandText);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">/*</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>SELECT DISTINCT</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[CustomerID], [t0].[CompanyName], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[ContactName], [t0].[ContactTitle], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[Address], [t0].[City], [t0].[Region], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[PostalCode], [t0].[Country], </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>[t0].[Phone], [t0].[Fax]</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>FROM [dbo].[Customers] AS [t0], [dbo].[Orders] AS [t1]</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>WHERE ([t1].[ShipCity] = @p0) </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>AND ([t0].[ContactTitle] = @p1) </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>AND ([t0].[City] = @p2) </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>AND ([t1].[CustomerID] = [t0].[CustomerID])<span style="mso-spacerun: yes;">          </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-size: 8pt; color: green; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>*/</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> cust <span style="color: blue;">in</span> query)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(cust);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">Cool eh!</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">This can be done because <strong style="mso-bidi-font-weight: normal;"><em style="mso-bidi-font-style: normal;">query</em></strong> is an expression tree and the SQL is only generated from the expression tree and the resulting SQL executed when we start to enumerate the query.</p>
<p><font face="Calibri" size="3"></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"> </p>
<p></font></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"> </p>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2008/07/composable-linq-to-sql/feed/</wfw:commentRss>
		</item>
		<item>
		<title>DataContext Attach Problem</title>
		<link>http://stephenedwards.virtualituk.com/2008/06/datacontext-attach-problem/</link>
		<comments>http://stephenedwards.virtualituk.com/2008/06/datacontext-attach-problem/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 20:11:09 +0000</pubDate>
		<dc:creator>stephen</dc:creator>
		
		<category><![CDATA[Data Platform]]></category>

		<category><![CDATA[Linq]]></category>

		<category><![CDATA[Attach]]></category>

		<category><![CDATA[Concurrency]]></category>

		<category><![CDATA[DataContext]]></category>

		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://stephenedwards.virtualituk.com/?p=4</guid>
		<description><![CDATA[
The DataContext keeps track of the state of business entities in LINQ To SQL, but many times the original DataContext is not available for updates and hence state information and changes made to the business entity is lost. 
This situation is common in an n-tier architecture where the business entity is serialized and sent across [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: small; font-family: Calibri;"></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: black; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The DataContext keeps track of the state of business entities in LINQ To SQL, but many times the original DataContext is not available for updates and hence state information and changes made to the business entity is lost. </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: black; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">This situation is common in an n-tier architecture where the business entity is serialized and sent across tiers. The entity is disconnected from the original data context and no change tracking is possible.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: black; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The Attach Method on the DataContext in LINQ To SQL allows you to re-attach an existing business entity that came from the database as well as information about the original state of the entity to help with optimistic concurrency.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: black; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The Attach method is provided on the ITable instance that supposedly allows re-attach an instance to be change tracked. Unfortunately this doesn’t work quite as expected.</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt; line-height: 14.25pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="color: black; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri;">The following code example illustrates:</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <strong><span style="color: #23778f;">AttachDemo</span></strong><span style="color: #23778f;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> DetachDemo()</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">string</span> userName <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #a31515;">&#8220;mytestuser&#8221;</span></strong>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Creates a user to use in demo.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>CreateDemoUser(userName);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">User</span></strong> user;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// SCENARIO 1</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> db <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> db<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> userName);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: green;">// Simulate detachment</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Make changes to the detached user.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>user<strong><span style="color: maroon;">.</span></strong>Password <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #a31515;">&#8220;s1password&#8221;</span></strong>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">try</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>Scenario_1_FailsToUpdateUser(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">catch</span> (<strong><span style="color: #23778f;">Exception</span></strong> exp)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><strong><span style="color: #a31515;">&#8220;FailsToUpdateUser method exception: {0}&#8221;</span></strong>, </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span>exp<strong><span style="color: maroon;">.</span></strong>Message);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// SCENARIO 2</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> db2 <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> db2<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> userName);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: green;">// Simulate detachment</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Make changes to the detached user.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>user<strong><span style="color: maroon;">.</span></strong>Password <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #a31515;">&#8220;s2password&#8221;</span></strong>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Update the detached user.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span></span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">Scenario_2_UpdateUser(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// SCENARIO 3</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">User</span></strong> originalUser;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> db3 <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> db3<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> <strong><span style="color: #a31515;">&#8220;mytestuser&#8221;</span></strong>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-tab-count: 2;">                </span><span style="color: green;">// originalUser = Detach&lt;User&gt;(user);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span>originalUser <strong><span style="color: maroon;">=</span></strong> db3<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>GetOriginalEntityState(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span></span><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">// Simulate detachment</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: green;">// This will simulate a situation where we hold original</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: green;">//<span style="mso-spacerun: yes;">  </span>state in some server side state cache or on the client tier</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: green;">//<span style="mso-spacerun: yes;">  </span>itself and thus is detached also.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>originalUser <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(originalUser);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Make changes to the detached user.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>user<strong><span style="color: maroon;">.</span></strong>Password <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #a31515;">&#8220;s3password&#8221;</span></strong>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: green;">// Update the detached user.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>Scenario_3_UpdateUser(user, originalUser);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: green;">// Serializes and deserializes the user. This has the effect</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: green;">//<span style="mso-spacerun: yes;">  </span>of detaching the user from the data context as would happen </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: green;">//<span style="mso-spacerun: yes;">  </span>in an n-tier application for example where the entities would</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: green;">//<span style="mso-spacerun: yes;">  </span>be serialized over the wire.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> T Detach<strong><span style="color: maroon;">&lt;</span></strong>T<strong><span style="color: maroon;">&gt;</span></strong>(T entity)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">DataContractSerializer</span></strong> dcs <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">DataContractSerializer</span></strong>(<span style="color: blue;">typeof</span>(T));</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><strong><span style="color: #23778f;">Stream</span></strong> mstream <strong><span style="color: maroon;">=</span></strong> <span style="color: blue;">new</span> <strong><span style="color: #23778f;">MemoryStream</span></strong>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>dcs<strong><span style="color: maroon;">.</span></strong>WriteObject(mstream, entity);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>mstream<strong><span style="color: maroon;">.</span></strong>Position <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: red;">0</span></strong>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">return</span> (T)dcs<strong><span style="color: maroon;">.</span></strong>ReadObject(mstream);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Scenario_1_FailsToUpdateUser(<strong><span style="color: #23778f;">User</span></strong> user)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> dc <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>Log <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>Out;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span></span><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">User</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> orgUser;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span>orgUser <strong><span style="color: maroon;">=</span></strong> dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> user<strong><span style="color: maroon;">.</span></strong>Username);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span></span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, orgUser);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(dc<strong><span style="color: maroon;">.</span></strong>GetChangeSet());</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>SubmitChanges();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Scenario_2_UpdateUser(<strong><span style="color: #23778f;">User</span></strong> user)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> dc <strong><span style="color: maroon;">=</span></strong> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>Log <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>Out;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span></span><strong><span style="font-size: 8pt; color: #23778f; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">User</span></strong><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"> orgUser;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> dca <strong><span style="color: maroon;">=</span></strong> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                    </span></span><span style="font-size: 8pt; color: blue; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">new</span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span><span style="color: green;">// dca.ObjectTrackingEnabled = false; // May save some overhead</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                    </span></span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">orgUser <strong><span style="color: maroon;">=</span></strong> dca<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> user<strong><span style="color: maroon;">.</span></strong>Username);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span></span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, orgUser);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(dc<strong><span style="color: maroon;">.</span></strong>GetChangeSet());</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>SubmitChanges();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Scenario_3_UpdateUser(<strong><span style="color: #23778f;">User</span></strong> user, <strong><span style="color: #23778f;">User</span></strong> originalUser)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> dc <strong><span style="color: maroon;">=</span></strong><span style="color: maroon;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>Log <strong><span style="color: maroon;">=</span></strong> <strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>Out;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, originalUser);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><strong><span style="color: #23778f;">Console</span><span style="color: maroon;">.</span></strong>WriteLine(dc<strong><span style="color: maroon;">.</span></strong>GetChangeSet());</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>dc<strong><span style="color: maroon;">.</span></strong>SubmitChanges();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <strong><span style="color: #23778f;">User</span></strong> GetUser(<span style="color: blue;">string</span> username)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">using</span> (<strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong> dc <strong><span style="color: maroon;">=</span></strong> </span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">new</span> <strong><span style="color: #23778f;">LinqToSQLDataClassesDataContext</span></strong>())</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">return</span> dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> username);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">            </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">The <span style="font-size: 8pt; color: blue; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">public</span><span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"> <span style="color: blue;">static</span> T Detach<strong><span style="color: maroon;">&lt;</span></strong>T<strong><span style="color: maroon;">&gt;</span></strong>(T entity)</span> method ensures that the entity passed in is detached from its data context by serializing it and then de-serializing it in memory using the <strong><span style="font-size: 8pt; color: #23778f; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">DataContractSerializer</span></strong> class from the <strong style="mso-bidi-font-weight: normal;"><span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">System<span style="color: maroon; mso-bidi-font-weight: bold;">.</span>Runtime<span style="color: maroon; mso-bidi-font-weight: bold;">.</span>Serialization</span></strong> namespace. This is what would generally happen in an n-tier architecture when entities are passed from tier to tier over the network. The important thing here is that they will no longer be attached to the originating data context and, of course, that originating data context can no longer track changes.</p>
<p><font face="Calibri" size="3"></p>
<h1 style="margin: 24pt 0cm 0pt;"><span style="font-size: large; color: #365f91; font-family: Cambria;">The <span style="mso-no-proof: yes;">Scenario_1_</span><span style="line-height: 115%; mso-bidi-font-size: 8.0pt;">FailsToUpdateUser</span> method</span></h1>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">Here we create a new data context and query it for the user that we want to update, in order to get the current state of the user in the database. This is then passed into the second parameter (“original”) of the Attach method. The first parameter is the updated user.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">What we want here, of course, is for the data context to compare the updated entity (first parameter) with the original (second parameter) and generate SQL accordingly.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">However, The <span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_1_FailsToUpdateUser</span> method throws an exception.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">The pertinent console output here is:</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">FailsToUpdateUser method exception: Cannot add an entity with a key that is already in use.</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">As you can see we get the exception “Cannot add an entity with a key that is already in use.” thrown by the <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, orgUser) </span>call.</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">I suspect that this is a bug, since one would expect the <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Attach(TEntity entity, TEntity original)</span> method to work in such a scenario. The error seems to be caused by the very fact that I have executed the query:</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt; text-indent: 36pt;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">orgUser <strong><span style="color: maroon;">=</span></strong> dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Single(u <strong><span style="color: maroon;">=&gt;</span></strong> u<strong><span style="color: maroon;">.</span></strong>Username <strong><span style="color: maroon;">==</span></strong> user<strong><span style="color: maroon;">.</span></strong>Username) </span></p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">in order to get the current state to pass into the second parameter of the attach method – all within the same data context.</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">If we simply call the <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user) </span>overload, the same error will occur.</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">Calling <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, <span style="color: blue;">true</span>)</span> (asModified=true) produces the exception:</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">“An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.”</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">This we would expect.</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">Calling <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, <span style="color: blue;">false</span>)</span> (asModified=false) again produces the exception:</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">“Cannot add an entity with a key that is already in use.”</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">Calling <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">dc<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>Attach(user, <span style="color: blue;">false</span>)</span> (asModified=false) without executing the preceding query produces no exception, but does not update the database!</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<h1 style="margin: 24pt 0cm 0pt;"><span style="font-size: large; color: #365f91; font-family: Cambria;">The <span style="mso-no-proof: yes;">Scenario_2_</span>UpdateUser method</span></h1>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">The difference between the <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_1_FailsToUpdateUser</span> method and the <span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_2_UpdateUser</span> method, which works, is simply that the query for the current state is executed on a separate data context. The console output for this method is:</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">{Inserts: 0, Deletes: 0, Updates: 1}</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">UPDATE [dbo].[Users]</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">SET [Password] = @p2</p>
<p class="MsoNoSpacing" style="margin: 0cm 0cm 0pt;">WHERE ([Username] = @p0) AND ([Password] = @p1) AND ([Active] = 1)</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">As we can see it has successfully worked out that we need an update and produces the minimal SQL to perform the task.</p>
<h1 style="margin: 24pt 0cm 0pt;"><span style="font-size: large; color: #365f91; font-family: Cambria;">The Scenario<span style="mso-no-proof: yes;">_3_UpdateUser</span> method</span></h1>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">This method works.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">This method differs from <span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_2_UpdateUser</span> method in that it requires an original user entity to be passed in, in addition to the updated entity.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">The original user entity passed in is generated by the original data context:</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span></span><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT">originalUser <strong><span style="color: maroon;">=</span></strong> db3<strong><span style="color: maroon;">.</span></strong>Users<strong><span style="color: maroon;">.</span></strong>GetOriginalEntityState(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: PT;" lang="PT"><span style="mso-spacerun: yes;">                </span></span><span style="font-size: 8pt; color: green; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">// Simulate detachment</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">                </span>user <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">In this case there is no significance to using the <span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">GetOriginalEntityState</span> method. I could equally have written:</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">originalUser <strong><span style="color: maroon;">=</span></strong> Detach<strong><span style="color: maroon;">&lt;</span><span style="color: #23778f;">User</span><span style="color: maroon;">&gt;</span></strong>(user);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">which would return a copy of the user. There is no significance to the fact that the originalUser was generated by the original context.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">This scenario would be typical of an architecture where the original state is stored on the client and returned along with changes to the data tier.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">So the reason that <span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_3_UpdateUser</span> method works is the same as the reason the <span style="font-size: 8pt; line-height: 115%; font-family: Consolas; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman';">Scenario_3_UpdateUser</span> method works. It is by virtue of the fact that the original state is not obtained by querying on the same data context that you want to attach it to for updating.</p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 10pt;">This does seem odd. Any enlightenment on why this is would be welcome! Microsoft&#8230;..?</p>
<p> </p>
<p></font></span></p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenedwards.virtualituk.com/2008/06/datacontext-attach-problem/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
