<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Discard changes in LINQ to SQL DataContext</title>
	<atom:link href="http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/feed" rel="self" type="application/rss+xml" />
	<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext</link>
	<description></description>
	<pubDate>Sat, 19 May 2012 09:04:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: umair</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-3063</link>
		<dc:creator>umair</dc:creator>
		<pubDate>Tue, 01 May 2012 16:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-3063</guid>
		<description>This works perfect for me. I hope in future, they will make this much simpler than this.

Thanks</description>
		<content:encoded><![CDATA[<p>This works perfect for me. I hope in future, they will make this much simpler than this.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reza</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-3008</link>
		<dc:creator>Reza</dc:creator>
		<pubDate>Mon, 30 Jan 2012 08:33:21 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-3008</guid>
		<description>Compelete code of my Datacontext:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;

namespace DataContextDiscardChanges
{
    partial class NorthwindDataContext
    {
        #region Public Members

        public void DiscardChanges()
        {
            this.DiscardUpdates();
            this.DiscardInserts();
            this.DiscardDeletes();
        }

        public void DiscardInserts(Func predicate = null)
        {
            if (predicate != null)
                discardInserts(this.GetChangeSet().Inserts.OfType().Where(predicate));
            else
                discardInserts(this.GetChangeSet().Inserts.OfType());
        }

        public void DiscardInserts()
        {
            discardInserts(this.GetChangeSet().Inserts);
        }

        public void DiscardDeletes(Func predicate = null)
        {
            if (predicate != null)
                discardDeletes(this.GetChangeSet().Deletes.OfType().Where(predicate));
            else
                discardDeletes(this.GetChangeSet().Deletes.OfType());
        }

        public void DiscardDeletes()
        {
            discardDeletes(this.GetChangeSet().Deletes);
        }

        public void DiscardUpdates(Func predicate = null)
        {
            if (predicate != null)
                discardUpdates(this.GetChangeSet().Updates.OfType().Where(predicate));
            else
                discardUpdates(this.GetChangeSet().Updates.OfType());
        }

        public void DiscardUpdates()
        {
            discardUpdates(this.GetChangeSet().Updates);
        }

        #endregion Public Members

        #region Private Memebers

        private void discardInserts(IEnumerable insertedEntities)
        {
            foreach (var insertedItem in insertedEntities)
            {
                this.GetTable(insertedItem.GetType()).DeleteOnSubmit(insertedItem);
            }
        }

        private void discardDeletes(IEnumerable deletedEntities)
        {
            foreach (var deletedItem in deletedEntities)
            {
                this.GetTable(deletedItem.GetType()).InsertOnSubmit(deletedItem);
            }
        }

        private void discardUpdates(IEnumerable updatedEntities)
        {
            foreach (var updatedItem in updatedEntities)
            {
                ModifiedMemberInfo[] modifiedMembers = this.GetTable(updatedItem.GetType()).GetModifiedMembers(updatedItem);

                foreach (var modifiedMember in modifiedMembers)
                {
                    updatedItem.GetType().GetProperty(modifiedMember.Member.Name).SetValue(updatedItem, modifiedMember.OriginalValue, null);
                }
            }
        }

        #endregion Private Memebers
    }
}</description>
		<content:encoded><![CDATA[<p>Compelete code of my Datacontext:</p>
<p>using System;<br />
using System.Collections;<br />
using System.Collections.Generic;<br />
using System.Data.Linq;<br />
using System.Linq;</p>
<p>namespace DataContextDiscardChanges<br />
{<br />
    partial class NorthwindDataContext<br />
    {<br />
        #region Public Members</p>
<p>        public void DiscardChanges()<br />
        {<br />
            this.DiscardUpdates();<br />
            this.DiscardInserts();<br />
            this.DiscardDeletes();<br />
        }</p>
<p>        public void DiscardInserts(Func predicate = null)<br />
        {<br />
            if (predicate != null)<br />
                discardInserts(this.GetChangeSet().Inserts.OfType().Where(predicate));<br />
            else<br />
                discardInserts(this.GetChangeSet().Inserts.OfType());<br />
        }</p>
<p>        public void DiscardInserts()<br />
        {<br />
            discardInserts(this.GetChangeSet().Inserts);<br />
        }</p>
<p>        public void DiscardDeletes(Func predicate = null)<br />
        {<br />
            if (predicate != null)<br />
                discardDeletes(this.GetChangeSet().Deletes.OfType().Where(predicate));<br />
            else<br />
                discardDeletes(this.GetChangeSet().Deletes.OfType());<br />
        }</p>
<p>        public void DiscardDeletes()<br />
        {<br />
            discardDeletes(this.GetChangeSet().Deletes);<br />
        }</p>
<p>        public void DiscardUpdates(Func predicate = null)<br />
        {<br />
            if (predicate != null)<br />
                discardUpdates(this.GetChangeSet().Updates.OfType().Where(predicate));<br />
            else<br />
                discardUpdates(this.GetChangeSet().Updates.OfType());<br />
        }</p>
<p>        public void DiscardUpdates()<br />
        {<br />
            discardUpdates(this.GetChangeSet().Updates);<br />
        }</p>
<p>        #endregion Public Members</p>
<p>        #region Private Memebers</p>
<p>        private void discardInserts(IEnumerable insertedEntities)<br />
        {<br />
            foreach (var insertedItem in insertedEntities)<br />
            {<br />
                this.GetTable(insertedItem.GetType()).DeleteOnSubmit(insertedItem);<br />
            }<br />
        }</p>
<p>        private void discardDeletes(IEnumerable deletedEntities)<br />
        {<br />
            foreach (var deletedItem in deletedEntities)<br />
            {<br />
                this.GetTable(deletedItem.GetType()).InsertOnSubmit(deletedItem);<br />
            }<br />
        }</p>
<p>        private void discardUpdates(IEnumerable updatedEntities)<br />
        {<br />
            foreach (var updatedItem in updatedEntities)<br />
            {<br />
                ModifiedMemberInfo[] modifiedMembers = this.GetTable(updatedItem.GetType()).GetModifiedMembers(updatedItem);</p>
<p>                foreach (var modifiedMember in modifiedMembers)<br />
                {<br />
                    updatedItem.GetType().GetProperty(modifiedMember.Member.Name).SetValue(updatedItem, modifiedMember.OriginalValue, null);<br />
                }<br />
            }<br />
        }</p>
<p>        #endregion Private Memebers<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reza</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-3007</link>
		<dc:creator>Reza</dc:creator>
		<pubDate>Sun, 29 Jan 2012 12:07:42 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-3007</guid>
		<description>hello every body

Discard Updated with Refresh method, reads orginal data from database and it's not good way to Discard in-memory objects

i'm using reflection to solve this problem:


public void DiscardUpdates()
        {
            var changeSet = this.GetChangeSet();
            foreach (var updatedItem in changeSet.Updates)
            {
                ModifiedMemberInfo[] modifiedMembers = this.GetTable(updatedItem.GetType()).GetModifiedMembers(updatedItem);

                foreach (var modifiedMember in modifiedMembers)
                {
                    updatedItem.GetType().GetProperty(modifiedMember.Member.Name).SetValue(updatedItem, modifiedMember.OriginalValue, null);
                }
            }
        }</description>
		<content:encoded><![CDATA[<p>hello every body</p>
<p>Discard Updated with Refresh method, reads orginal data from database and it&#8217;s not good way to Discard in-memory objects</p>
<p>i&#8217;m using reflection to solve this problem:</p>
<p>public void DiscardUpdates()<br />
        {<br />
            var changeSet = this.GetChangeSet();<br />
            foreach (var updatedItem in changeSet.Updates)<br />
            {<br />
                ModifiedMemberInfo[] modifiedMembers = this.GetTable(updatedItem.GetType()).GetModifiedMembers(updatedItem);</p>
<p>                foreach (var modifiedMember in modifiedMembers)<br />
                {<br />
                    updatedItem.GetType().GetProperty(modifiedMember.Member.Name).SetValue(updatedItem, modifiedMember.OriginalValue, null);<br />
                }<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andre</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-2996</link>
		<dc:creator>Andre</dc:creator>
		<pubDate>Fri, 13 Jan 2012 14:00:32 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-2996</guid>
		<description>It's an incredible article. It's such a simple solution for all my problems with discarding data contexts. 

Thanks so much!</description>
		<content:encoded><![CDATA[<p>It&#8217;s an incredible article. It&#8217;s such a simple solution for all my problems with discarding data contexts. </p>
<p>Thanks so much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Usman</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-2879</link>
		<dc:creator>Usman</dc:creator>
		<pubDate>Tue, 29 Nov 2011 23:00:43 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-2879</guid>
		<description>Thanks! Good to know this exists! My small blogpost on this
http://usman-suglatwala.blogspot.com/2011/11/discarding-changes-in-linq-memory.html</description>
		<content:encoded><![CDATA[<p>Thanks! Good to know this exists! My small blogpost on this<br />
<a href="http://usman-suglatwala.blogspot.com/2011/11/discarding-changes-in-linq-memory.html" onclick="javascript:pageTracker._trackPageview('/outbound/comment/http://usman-suglatwala.blogspot.com/2011/11/discarding-changes-in-linq-memory.html');" rel="nofollow">http://usman-suglatwala.blogspot.com/2011/11/discarding-changes-in-linq-memory.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimbo</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-1433</link>
		<dc:creator>Jimbo</dc:creator>
		<pubDate>Wed, 26 Oct 2011 14:55:57 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-1433</guid>
		<description>Glad I found this, creating a new DataContext was just not an option for me - your code works great, thanks :)</description>
		<content:encoded><![CDATA[<p>Glad I found this, creating a new DataContext was just not an option for me - your code works great, thanks <img src='http://graemehill.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John P.</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-1398</link>
		<dc:creator>John P.</dc:creator>
		<pubDate>Tue, 27 Sep 2011 20:15:28 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-1398</guid>
		<description>I'd been working on this for hours before finding this post.  Works Great!  Thanks.</description>
		<content:encoded><![CDATA[<p>I&#8217;d been working on this for hours before finding this post.  Works Great!  Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-900</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Fri, 11 Feb 2011 20:17:26 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-900</guid>
		<description>the data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);
works
but
data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);
does not work for me
.net4.0
the data.getchangeset() will show the same number of deletes   (1)
&#38; of course at a subsequent SubmitChanges will operate on the database the pending delete
anybody could help?</description>
		<content:encoded><![CDATA[<p>the data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);<br />
works<br />
but<br />
data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);<br />
does not work for me<br />
.net4.0<br />
the data.getchangeset() will show the same number of deletes   (1)<br />
&amp; of course at a subsequent SubmitChanges will operate on the database the pending delete<br />
anybody could help?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-899</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 11 Feb 2011 19:48:27 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-899</guid>
		<description>fantastic article - thank you!  


Question, is there a problem with the following code? it seemed to work in my testing

		static void DiscardChanges()
		{
			var changes = data.GetChangeSet();

			//insert the deletes
			foreach (var insertion in changes.Inserts)
				data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);

			//delete the inserts
			foreach (var deletion in changes.Deletes)
				data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);

			//undo the updates
			foreach (var update in changes.Updates)
				data.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, changes.Updates);
		}</description>
		<content:encoded><![CDATA[<p>fantastic article - thank you!  </p>
<p>Question, is there a problem with the following code? it seemed to work in my testing</p>
<p>		static void DiscardChanges()<br />
		{<br />
			var changes = data.GetChangeSet();</p>
<p>			//insert the deletes<br />
			foreach (var insertion in changes.Inserts)<br />
				data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);</p>
<p>			//delete the inserts<br />
			foreach (var deletion in changes.Deletes)<br />
				data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);</p>
<p>			//undo the updates<br />
			foreach (var update in changes.Updates)<br />
				data.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, changes.Updates);<br />
		}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shalu</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-784</link>
		<dc:creator>Shalu</dc:creator>
		<pubDate>Fri, 22 Oct 2010 12:28:15 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-784</guid>
		<description>Awesome article. The c# code for discarding the changes helped me so much. Thanks for posting this article.</description>
		<content:encoded><![CDATA[<p>Awesome article. The c# code for discarding the changes helped me so much. Thanks for posting this article.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

