<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Write Less, Do More, &lt;br&gt;But Keep It Simple : LINQ to SQL</title><link>http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx</link><description>Tags: LINQ to SQL</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Converting web site projects from Visual Studio 2008 Beta 2 to Visual Studio 2008 RTM</title><link>http://weblogs.asp.net/bschooley/archive/2007/11/21/converting-web-site-projects-from-visual-studio-2008-beta-2-to-visual-studio-2008-rtm.aspx</link><pubDate>Wed, 21 Nov 2007 15:44:38 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5305627</guid><dc:creator>bschooley</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=5305627</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=5305627</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/11/21/converting-web-site-projects-from-visual-studio-2008-beta-2-to-visual-studio-2008-rtm.aspx#comments</comments><description>&lt;p&gt;The good news is that this conversion was a lot less painful than the move from the final beta of .Net 2.0 to it's released version. The only breaking changes I ran into were minor changes in LINQ to SQL. While I was converting web site projects, I imagine since my breaking changes were limited to LINQ to SQL, that these changes would hold true for other projects as well.&lt;/p&gt;  &lt;p&gt;First, this is just one scenario, your personal experience may vary.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;LINQ to SQL Designer and the DBML File Encoding&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This change is self correcting for the most part. If you attempt to open a .dbml file or compile your app before making this change you will get a &amp;quot;Cannot load ...&amp;quot; error. When you click the &amp;quot;Ok&amp;quot; button, the dbml file will automatically be opened with the xml editor. All you need to do is click save, wait for the pause as it regenerates the classes, and then close the file. The error has to do with the utf encoding of the xml file.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Web.config Assembly Reference Change for System.Data.DataSetExtensions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Open your web.config file and find and change the version number in this line from 2.0:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;assembly&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;/&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;to 3.5:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;assembly&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;then save and close.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LINQ to SQL Add and Remove Method Name Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From here you can attempt to compile to find the remaining changes you need to make. The first ones you'll run into are the Add and Remove methods of the LINQ to SQL Table&amp;lt;&amp;gt; objects. The &lt;strong&gt;Add &lt;/strong&gt;method was changed to &lt;strong&gt;InsertOnSubmit &lt;/strong&gt;and the &lt;strong&gt;Remove &lt;/strong&gt;method was changed to &lt;strong&gt;DeleteOnSubmit&lt;/strong&gt;. While more wordy, the change was made to make them more explicitly describe their behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LINQ to SQL OnValidate Partial Method Change&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you were using the OnValidate partial method, then you'll need to make a small change. But this time you may want to make some alterations to your code logic as well.&lt;/p&gt;

&lt;p&gt;To put it simply, the OnValidate now has a ChangeAction parameter. This is a great change as it basically informs the method about the reason for the call. ChangeAction has four values: Insert, Update, Delete and None.&lt;/p&gt;

&lt;p&gt;So, find and change all your:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OnValidate()&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;to:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OnValidate(&lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt; action)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Depending on what your doing you may want to consider wrapping the contents of your method in a simple:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (action == &lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt;.Insert || action == &lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt;.Update)
{&lt;br /&gt;...&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;You may want to skip that for now and revisit it when you are ready to think through the implications of this change. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My test applications made use of extension methods, nested master pages and a few other 3.5 features, but so far I haven't run into any other breaking changes. If I run across more, I'll update this blog post.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5305627" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>LINQ : Paging and sorting LINQ Queries with a custom QueryDataSource</title><link>http://weblogs.asp.net/bschooley/archive/2007/11/03/linq-paging-and-sorting-linq-queries-with-a-custom-querydatasource.aspx</link><pubDate>Sat, 03 Nov 2007 07:48:50 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4872204</guid><dc:creator>bschooley</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4872204</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4872204</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/11/03/linq-paging-and-sorting-linq-queries-with-a-custom-querydatasource.aspx#comments</comments><description>&lt;p&gt;So you've got a LINQ query and now you want to drop it on a GridView. If you assign it to the datasource and bind it, you'll get data, but if you've define a page size or sort expressions, as soon as you click something you'll get &amp;quot;&lt;i&gt;The GridView fired event PageIndexChanging which wasn't handled.&lt;/i&gt;&amp;quot; &lt;/p&gt;  &lt;p&gt;At this point you have two options, you can punish yourself by writing and wiring up all the paging and sorting events, or you could try using the LinqDataSource.&lt;/p&gt;  &lt;p&gt;The LinqDataSource is nice in that it will handle all the paging, sorting and more, but it comes at a cost. For starters, all that great intellisense you get when writing the query is out-the-window. I won't even broach the subject of business versus data versus UI layers. Let's just say the LinqDataSource is the Linq equivalent of the SqlDataSource, something many people avoid at all costs.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;There must be another way.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Paging and Sorting are basic application plumbing problems and not something you should be concerned with when working at the page level. After trying to use the LinqDataSource a few times, I figured there must be a better way to do this. I wanted to write my query with all the benefits of Intellisense and in some cases I needed to conditionally add filters for search criteria. What I needed was a DataSource that I could pass a query to and let it handle the paging and sorting as needed by the GridView.&lt;/p&gt;  &lt;p&gt;Writing the QueryDataSource control turned out to be easier than I expected. My first break was finding a nice simple &lt;a href="http://weblogs.asp.net/pwilson/archive/2006/05/17/446919.aspx"&gt;example DataSource&lt;/a&gt; by Paul Wilson for his &lt;a href="http://www.ormapper.net/"&gt;WIlsonORMapper&lt;/a&gt;. And second, finding &lt;a href="http://weblogs.asp.net/rajbk/archive/2007/09/18/dynamic-string-based-queries-in-linq.aspx"&gt;this article&lt;/a&gt; by Raj Kaimal where he talks about Microsoft's Dynamic Expressions API that was released in the &lt;a href="http://msdn2.microsoft.com/en-us/bb330936.aspx"&gt;LINQ and C# language samples for Visual Studio 2008 beta 2&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;After a couple hours of playing around I came up with the QueryDataSource control.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;QueryDataSource&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This control makes use of three simple features of LINQ queries, Skip, Take and OrderBy. I ended up not being able to &amp;quot;generically&amp;quot; apply these commands to IQueryable&amp;lt;T&amp;gt; expressions because during processing I don't know anything about &amp;lt;T&amp;gt;. That's where the Dynamic Expressions API library comes in. Quoting from the API docs regarding it's extension methods...&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;These methods correspond to their System.Linq.Queryable counterparts, except that they operate on IQueryable instead of IQueryable&amp;lt;T&amp;gt; and use strings instead of lambda expressions to express predicates, selectors, and orderings. IQueryable is the non-generic base interface for IQueryable&amp;lt;T&amp;gt;, so the methods can be used even when T isn&amp;#x2019;t known on beforehand...&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Here's the heart of the QueryDataSource control, it's ExecuteSelect method:&lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; arguments.RaiseUnsupportedCapabilitiesError(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; QueryDataSourceLoadSelectQueryEventArgs e = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; QueryDataSourceLoadSelectQueryEventArgs(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;._owner.Query);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.OnLoadSelectQuery(e);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; IQueryable query = &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;._owner.Query;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (arguments.RetrieveTotalRowCount)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; arguments.TotalRowCount = query.Count();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(arguments.SortExpression))&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; query = query.OrderBy(arguments.SortExpression);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (arguments.StartRowIndex &amp;gt; 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; query = query.Skip(arguments.StartRowIndex);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (arguments.MaximumRows &amp;gt; 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; query = query.Take(arguments.MaximumRows);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; query;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As with any paging function, this hits the database twice. First to get the TotalRowCount by executing &amp;quot;query.Count()&amp;quot; and then when it returns the query after adding the OrderBy, Skip and Take as required. Technically it doesn't actually execute the query a second time here, as it is passes the modified query object to the GridView and basically defers the execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Using the QueryDataSource&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a simple example web page using the QueryDataSource control.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;%@ Page Language=&lt;span style="color: #006080"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; MasterPageFile=&lt;span style="color: #006080"&gt;&amp;quot;~/Default.Master&amp;quot;&lt;/span&gt; %&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;script runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ds_Employees_OnLoadSelectQuery(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; source, QueryDataSourceLoadSelectQueryEventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; DB.NW.DataContext dc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.DataContext();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; e.Query = dc.Employees;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;asp:Content ID=&lt;span style="color: #006080"&gt;&amp;quot;c_Body&amp;quot;&lt;/span&gt; ContentPlaceHolderID=&lt;span style="color: #006080"&gt;&amp;quot;cph_Body&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;mps:QueryDataSource ID=&lt;span style="color: #006080"&gt;&amp;quot;ds_Employees&amp;quot;&lt;/span&gt; OnLoadSelectQuery=&lt;span style="color: #006080"&gt;&amp;quot;ds_Employees_OnLoadSelectQuery&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;asp:GridView ID=&lt;span style="color: #006080"&gt;&amp;quot;gv_Employees&amp;quot;&lt;/span&gt; DataSourceID=&lt;span style="color: #006080"&gt;&amp;quot;ds_Employees&amp;quot;&lt;/span&gt; AutoGenerateColumns=&lt;span style="color: #006080"&gt;&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; AllowPaging=&lt;span style="color: #006080"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; AllowSorting=&lt;span style="color: #006080"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; PageSize=&lt;span style="color: #006080"&gt;&amp;quot;5&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;Columns&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;asp:BoundField HeaderText=&lt;span style="color: #006080"&gt;&amp;quot;First Name&amp;quot;&lt;/span&gt; DataField=&lt;span style="color: #006080"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt; SortExpression=&lt;span style="color: #006080"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;asp:BoundField HeaderText=&lt;span style="color: #006080"&gt;&amp;quot;Last Name&amp;quot;&lt;/span&gt; DataField=&lt;span style="color: #006080"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt; SortExpression=&lt;span style="color: #006080"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;/Columns&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;/asp:GridView&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This is about as simple as it gets. In your page markup you only need to provide the name of an event handler for the OnLoadSelectQuery event. And then in your code you create your query or retrieve it from another application layer and assign it to the EventArg's Query property.&lt;/p&gt;

&lt;p&gt;I'm purposely not setting the query during OnLoad or other page life-cycle event because I want to wait until the GridView actually requests the data from the QueryDataSource. This should allow all the other page life-cycle events to process first so that if you change any variables or control values along the way, you won't need to rebind your GridView.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;QueryDataSource isn't just for LINQ to SQL&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since we're just using standard LINQ expressions for paging and sorting, the QueryDataSource will actually work with any object that implements IQueryable or IQueryable&amp;lt;T&amp;gt;.&lt;/p&gt;

&lt;p&gt;Here's an example that runs over a simple string array.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;%@ Page Language=&lt;span style="color: #006080"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; MasterPageFile=&lt;span style="color: #006080"&gt;&amp;quot;~/Default.Master&amp;quot;&lt;/span&gt; %&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;script runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ds_Names_OnLoadSelectQuery(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; source, QueryDataSourceLoadSelectQueryEventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] names = { &lt;span style="color: #006080"&gt;&amp;quot;Tom&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Dick&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Harry&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Mary&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Jay&amp;quot;&lt;/span&gt; };&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; e.Query = from n &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; names.AsQueryable()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; select &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; { FirstName = n };&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;lt;asp:Content ID=&lt;span style="color: #006080"&gt;&amp;quot;c_Body&amp;quot;&lt;/span&gt; ContentPlaceHolderID=&lt;span style="color: #006080"&gt;&amp;quot;cph_Body&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;mps:QueryDataSource ID=&lt;span style="color: #006080"&gt;&amp;quot;ds_Names&amp;quot;&lt;/span&gt; OnLoadSelectQuery=&lt;span style="color: #006080"&gt;&amp;quot;ds_Names_OnLoadSelectQuery&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;asp:GridView ID=&lt;span style="color: #006080"&gt;&amp;quot;gv_Names&amp;quot;&lt;/span&gt; DataSourceID=&lt;span style="color: #006080"&gt;&amp;quot;ds_Names&amp;quot;&lt;/span&gt; AutoGenerateColumns=&lt;span style="color: #006080"&gt;&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; AllowPaging=&lt;span style="color: #006080"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; AllowSorting=&lt;span style="color: #006080"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; PageSize=&lt;span style="color: #006080"&gt;&amp;quot;3&amp;quot;&lt;/span&gt; runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;Columns&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;asp:BoundField HeaderText=&lt;span style="color: #006080"&gt;&amp;quot;First Name&amp;quot;&lt;/span&gt; DataField=&lt;span style="color: #006080"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt; SortExpression=&lt;span style="color: #006080"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;/Columns&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;/asp:GridView&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This could be a LINQ to XML, LINQ to Amazon, or just a LINQ over any collection of objects.&lt;/p&gt;

&lt;p&gt;The possibilities are endless...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Downloading and Installing&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I've created a simple ASP.Net Server Control project which you can compile and then reference in your web project or you can copy the two cs files to your app_code folder.&lt;/p&gt;

&lt;p&gt;You can download the zip file &lt;strong&gt;&lt;a href="http://www.schooley.us/downloads/QueryDataSource.zip"&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you compile the project and copy the dll to your bin folder or if you add it to your solution and add a project reference, then you'll need to add the following to your web.config file to register the control.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;system.web&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;pages&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;controls&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;add tagPrefix=&lt;span style="color: #006080"&gt;&amp;quot;mps&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt;=&lt;span style="color: #006080"&gt;&amp;quot;mps.WebControls&amp;quot;&lt;/span&gt; assembly=&lt;span style="color: #006080"&gt;&amp;quot;mps.WebControls.QueryDataSource&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &amp;lt;/controls&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &amp;lt;/pages&amp;gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;lt;/system.web&amp;gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If you include the cs files in your app_code folder, then add the line above but leave out the assemply attribute.&lt;/p&gt;

&lt;p&gt;You can change the tagPrefix as well, but if you want another namespace, you'll need to alter the source files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;The future of QueryDataSource&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If someone else doesn't beat me to it, I'll probably add the ability to provide a TypeName, SelectMethod and SelectParameters much like an ObjectDataSource to allow direct access to an object layer that provides an IQueryable return value.&lt;/p&gt;

&lt;p&gt;Beyond that, I may at some point extend the control to support separate Insert, Update and Delete queries. But since I seldom if ever use that type of functionality, I may never get around to it.&lt;/p&gt;

&lt;p&gt;If anyone takes and extends this in either of these or other ways, please let me know.&lt;/p&gt;

&lt;p&gt;Enjoy...&lt;/p&gt;

&lt;p&gt;--Bernal&lt;/p&gt;

&lt;div&gt;&amp;#xA0;&lt;/div&gt;

&lt;div&gt;&amp;#xA0;&lt;/div&gt;

&lt;div&gt;&amp;#xA0;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4872204" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>MSDN's 101 LINQ Samples</title><link>http://weblogs.asp.net/bschooley/archive/2007/10/25/msdn-s-101-linq-samples.aspx</link><pubDate>Thu, 25 Oct 2007 16:24:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4744429</guid><dc:creator>bschooley</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4744429</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4744429</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/10/25/msdn-s-101-linq-samples.aspx#comments</comments><description>&lt;p&gt;I've been reading about, studying, and using LINQ to SQL for a couple months now and after all this time I somehow never ran across this MSDN web page.&lt;/p&gt;  &lt;h4&gt;&lt;a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx"&gt;101 LINQ Samples&lt;/a&gt; - C# Version&lt;/h4&gt;  &lt;h4&gt;&lt;a href="http://msdn2.microsoft.com/en-us/bb688088.aspx"&gt;101 LINQ Samples&lt;/a&gt; - VB Version (has lots more content and sections on LINQ to SQL, etc)&lt;/h4&gt;  &lt;p&gt;After just a few minutes of digging through it's samples, I've already learned more than I care to admit. These samples help to show that LINQ is a lot deeper and more expressive than you might think at first glance.&lt;/p&gt;  &lt;p&gt;[Update: 10-26]&lt;/p&gt;  &lt;p&gt;After some additional searches and aimless browsing on the MSDN site I discovered there are multiple flavors and versions of the &amp;quot;101 LINQ Samples&amp;quot;. I've updated the links above to include what I think are the most current pages.&lt;/p&gt;  &lt;p&gt;Here are some other links to similar &amp;quot;101 LINQ Samples&amp;quot; pages that I think are out of date but may still have nuggets of value buried in them:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/adonet/archive/2007/03/13/101-linq-samples-updated.aspx"&gt;101 LINQ to Entities Samples&lt;/a&gt; - Downloadable Zip from the ADO.Net Team (out-of-date)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2007/03/04/samples-update.aspx"&gt;LINQ Samples from Orcas March CTP&lt;/a&gt; - Another Downloadable Zip (out-of-date)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://download.microsoft.com/download/8/0/e/80e3f901-9bcc-4b8b-ba66-c49e24559fbc/vcs_samples_vs2008_beta2_02.exe"&gt;LINQ and language samples for Visual Studio 2008 Beta 2&lt;/a&gt; - Executable samples package from &lt;a href="http://msdn2.microsoft.com/en-us/vbasic/bb330936.aspx"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4744429" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category></item><item><title>LINQ to SQL : Handling disconnected CRUD operations in ASP.Net</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/29/linq-to-sql-handling-disconnected-crud-operations-in-asp-net.aspx</link><pubDate>Sat, 29 Sep 2007 21:37:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4248179</guid><dc:creator>bschooley</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4248179</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4248179</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/29/linq-to-sql-handling-disconnected-crud-operations-in-asp-net.aspx#comments</comments><description>&lt;p&gt;Most LINQ to SQL examples you find on the net tend to be written in a way that assumes a connected environment. I've found some blogs and posts that also talk about disconnected operations, but many of those are coming from the perspective of multiple tiers where the entity objects are being passed between tiers. &lt;/p&gt;  &lt;p&gt;For this example, I've got a page that handles simple CRUD operations for a single employee record. It takes a query string parameter of &amp;quot;id&amp;quot;. When it's not passed or &amp;quot;0&amp;quot; is passed, then it assumes you are going to be adding a new employee record, otherwise it loads the employee and allows you to edit it.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Note: I'm using module (page) level variables that are read from and stored in ViewState by overridden SaveViewState and LoadViewState. See my blog &lt;a href="http://weblogs.asp.net/bschooley/archive/2007/09/29/asp-net-clean-up-your-use-of-viewstate.aspx" target="_blank"&gt;post on the subject&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Here's the OnLoad method... &lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnLoad(EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!IsPostBack)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; _EmployeeID = Convert.ToInt32(Request.QueryString[&lt;span style="color: #006080"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;] ?? &lt;span style="color: #006080"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_EmployeeID != 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; DB.NW.DataContext dc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.DataContext();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; DB.NW.Employee employee = dc.Employees.Single(em =&amp;gt; em.EmployeeID == _EmployeeID);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; _Version = employee.Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; tb_FirstName.Text = employee.FirstName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; tb_LastName.Text = employee.LastName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; tb_HomePhone.Text = employee.HomePhone;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.OnLoad(e);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I'm going to assume you seen the reading of a single record from a LINQ to SQL DataContext, if not Scott Guthrie has a good &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;introduction to LINQ to SQL&lt;/a&gt; you should read first. I'm also using the null coalescing operator, if that's new to you then again, Scott &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx" target="_blank"&gt;covered that too&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Notice here that I'm storing two values to local variables (ie ViewState). The _EmployeeID and the _Version. The version field is a row TimeStamp field I've added to the Employee table to facilitate concurrency checking on updates. I'll talk more about this later in this post.&lt;/p&gt;

&lt;p&gt;Now for the good stuff. Here's the Save method...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Save(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; source, EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; DB.NW.DataContext dc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.DataContext();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_EmployeeID == 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; dc.Employees.Add(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; dc.SubmitChanges();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; Response.Redirect(&lt;span style="color: #006080"&gt;&amp;quot;~/Employees.aspx&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (Exception ex)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; lbl_Message.Text = ex.Message;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Let's break it down. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;When adding a new employee record.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start by creating a DataContext and a new Employee object. Then set the properties based on the form control values and &amp;quot;Add&amp;quot; the employee object to the DataContext's Employees table. Finish with a SubmitChanges().&lt;/p&gt;

&lt;p&gt;&lt;em&gt;When updating things get a bit more interesting.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start by setting the id and version properties of the new employee object and then &amp;quot;Attach&amp;quot; the it to the DataContext's Employees table. After it is attached, move the form control values to the employee object and finish by calling SubmitChanges().&lt;/p&gt;

&lt;p&gt;It is important to understand why this sequence of steps is required for an update. The attach step is the key and it relates to how the DataContext tracks changes to objects. The &amp;quot;Attach&amp;quot; tells the DataContext that we have this employee object with an id of _EmployeeID and a row version timestamp of _Version and we want you to know about it and track any further changes so you can save them. When calling SubmitChanges(), the DataContext will create it's update statement and only include fields that changed after the object was attached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variations on the Update&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the code above is a complete workable approach, there are some assumptions and pre-requisites that led to it. First, the table has a timestamp field to track the row version for concurrency checking. If your table doesn't have one, in most cases I'd suggest adding it. If that's not possible then you have a few options, all of which involve more work on your part. &lt;/p&gt;

&lt;p&gt;The easiest option is to turn off concurrency checking by changing the &amp;quot;update check&amp;quot; attribute for each field and setting it to &amp;quot;never&amp;quot;. By default, LINQ to SQL sets the attribute to &amp;quot;always&amp;quot;. This will allow the above code above to continue to work (just remove the lines dealing with the Version field). But removing concurrency checking isn't a good idea unless you intend to handle it yourself by adding your own code for it to the update process above.&lt;/p&gt;

&lt;p&gt;What you shouldn't do is simply re-retrieve the employee record and change its properties. If you are handling the currency checking on your own via a property by property compare or a last updated date time value, you may think you are covered. The problem is that you are either trying to ignore concurrency checking or you are doing your own concurrency checking while still allowing LINQ to SQL to do it's concurrency checks as well so you may get a LINQ to SQL concurrency error if two users try to update a record a the same time, even if you are doing some amount of your own checking. It the end you either need to use LINQ to SQL's concurrency process or to turn it off.&lt;/p&gt;

&lt;p&gt;Here's a bit more food for thought on this subject. Below are approaches to updates and the SQL statements generated by LINQ to SQL.&lt;/p&gt;

&lt;p&gt;First, the code from above that makes use of a Version field and LINQ to SQL's concurrency checking...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [LastName] = @p2, [FirstName] = @p3, [HomePhone] = @p4&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([Version] = @p1)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Turning off update checks on your fields and removing the Version field leaves you vulnerable to concurrency issues, but here's the code...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [LastName] = @p1, [FirstName] = @p2, [HomePhone] = @p3&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE [EmployeeID] = @p0&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;And last, if you leave the Update Check on and re-retrieve the employee record the code ...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee = dc.Employees.Single(em =&amp;gt; em.EmployeeID == _EmployeeID);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates... &lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [HomePhone] = @p15&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([LastName] = @p1) AND ([FirstName] = @p2) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([Title] = @p3) AND ([TitleOfCourtesy] = @p4) AND ([BirthDate] = @p5) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;AND ([HireDate] = @p6) AND ([Address] = @p7) AND ([City] = @p8) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([Region] IS NULL) AND ([PostalCode] = @p9) AND ([Country] = @p10) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;AND ([HomePhone] = @p11) AND ([Extension] = @p12) AND ([ReportsTo] = @p13) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([PhotoPath] = @p14)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the last of the three update statements, LINQ to SQL is sending every field as part of the update statement's where clause. I'll also point out here that in this case, LINQ to SQL does know that the FirstName and LastName fields were not changed to new values and therefore didn't include those in the update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Note on Change Tracking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a more advanced and situational concept, but one of the interesting possibilities that LINQ to SQL enables is the ability to build change tracking into the SubmitChanges() process. The basic idea is that any time you make changes to the database, you first go through the list of changes being made and record them in a generic change log table. I'll be writing about this idea in a later post, but for now, I'll throw out a warning that may effect how you handle updates. &lt;/p&gt;

&lt;p&gt;When using the model I suggest at the beginning of this post, the DataContext doesn't know the original values of the three fields (FirstName, LastName and HomePhone) that are being set. As far as the DataContext is aware, the original values are null so, even if nothing is really changing, the fields will be added to the change set. This means when you go through the change set during SubmitChanges() the original values will be empty. So, if you want to really know, at a field level what is truly changing, you'll need to take a different approach that what is shown above. &lt;/p&gt;

&lt;p&gt;Here I've stored the original values for all fields that can be updated and I set the employee object to those values before attaching it to the Employees table.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = _FirstName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = _LastName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = _HomePhone;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [HomePhone] = @p2&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([Version] = @p1)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In this case, LINQ to SQL knows the original values for the properties and as in the third example I gave earlier it generates the update set statement with the HomePhone, which was the only field that changed. During the SubmitChanges, if you look into the ChangeSet and retrieve the list of changed properties, you'll find that only the HomePhone is given and it has the original and new values. This will enable you to record the changes in your generic change log table.&lt;/p&gt;

&lt;p&gt;So, in order to do both change tracking at the field level and concurrency checking in a disconnected environment such as ASP.Net, you need to be able to create your employee object and set all the properties that you may be changing to their original values before you attach and then update those properties.&lt;/p&gt;

&lt;p&gt;I'll be writing a post later that goes into more detail and presents the pros and cons on a few different approaches to tracking original values for objects that you will be updating. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4248179" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>LINQ to SQL : Start with a good foundation by defining a custom DataContext</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/28/linq-to-sql-start-with-a-good-foundation-by-defining-a-custom-datacontext.aspx</link><pubDate>Fri, 28 Sep 2007 05:43:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4224356</guid><dc:creator>bschooley</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4224356</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4224356</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/28/linq-to-sql-start-with-a-good-foundation-by-defining-a-custom-datacontext.aspx#comments</comments><description>&lt;P&gt;This is my first post in a what will become a series of posts on LINQ to SQL. As the title of my blog suggests, I'm going to be "thinking out loud" as I write these and I'm hoping for feedback from others on the pro's and con's of the approaches I'm suggesting. That said, the point of this series of posts will be to define a loose set of best practices around developing with LINQ to SQL in an ASP.Net web environment. The first couple posts will be pretty simplistic but I wanted to get these in place first so I can refer to the concepts later without having to go into detail.&lt;/P&gt;
&lt;P&gt;On with the topic at hand...&lt;/P&gt;
&lt;P&gt;When starting a new project with LINQ to SQL, it's a good idea to wrap the DataContext with your own class that inherits from the DataContext generated by the designer (or SQLMetal).&lt;/P&gt;
&lt;P&gt;Before we do that though, let's take a small step back and talk about namespaces. In the designer, if you click on design surface background, you'll select the DataContext. In the properties window there will be two entries related to namespaces. One is the Context Namespace and the other is the Entity Namespace. Typically you'd put both the generated DataContext and the entity classes within the same namespace, but since we're going to be creating our own DataContext that inherits from the one that is generated for us, I'd suggest that we set these up with different namespaces.&lt;/P&gt;
&lt;P&gt;I like to have my data layer to have its own namespace and as such I tend to go with a DB.such_and_such namespace where such_and_such is the name of our database or other logical context name. I'll be using the northwind database for this series of posts, so the logical namespace name is DB.NW. This is what we should put in the Entity Namespace property, but for the Context Namespace, we should set the value to DB.NW.Base to leave room for our custom version in the DB.NW namespace. With our DataContext's name set as simply DataContext as well, we'll get the following generated for us by the designer...&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW.Base &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public partial class&lt;/FONT&gt; DataContext : System.Data.Linq.DataContext &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;} &lt;BR&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Table...] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public partial class&lt;/FONT&gt; table_name... etc. &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;Next we need to create a new class file in app_code. In this case I'll keep the name the same as the namespace it contains and go with DB.NW.cs. &lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Data.Linq; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Data.Linq.Mapping; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Linq; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Runtime.Serialization; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Reflection; &lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public class&lt;/FONT&gt; DataContext : DB.NW.Base.DataContext &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;Now, it may seem silly at this point to go through all this but doing this now, even with no custom or overridden methods, will save us from having to change our code later that when we need to override methods such as SubmitChanges()..&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4224356" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category></item></channel></rss>