<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/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>Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx</link><description>Linq to Sql does not come with a function like .Delete(ID) which allows you to delete an entity using it&amp;rsquo;s primary key. You have to first get the object that you want to delete and then call .DeleteOnSubmit(obj) to queue it for delete. Then you</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1731746</link><pubDate>Mon, 12 Oct 2009 01:05:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731746</guid><dc:creator>PimpThisBlog.com</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from PimpThisBlog.com&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1731746" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1692152</link><pubDate>Sun, 10 May 2009 19:40:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1692152</guid><dc:creator>Reza</dc:creator><description>&lt;p&gt;Great Post! Do you have any idea how we can do the same thing for tables with more than one primary key?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1692152" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1671492</link><pubDate>Wed, 11 Feb 2009 21:15:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1671492</guid><dc:creator>x</dc:creator><description>&lt;p&gt;you should not do the reflection yourself.&lt;/p&gt;
&lt;p&gt;What about&lt;/p&gt;
&lt;p&gt;table.Context.Mapping.GetTable(typeof(TTable)).TableName&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1671492" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1661591</link><pubDate>Wed, 14 Jan 2009 17:24:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1661591</guid><dc:creator>Yuri</dc:creator><description>&lt;p&gt;Very nice! I&amp;#39;ve used this and it works perfectly!&lt;/p&gt;
&lt;p&gt;Thanks!!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1661591" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1657785</link><pubDate>Fri, 26 Dec 2008 21:10:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1657785</guid><dc:creator>amyo</dc:creator><description>&lt;p&gt;This is one solution to avoid roundtrip, you can use store procedure to delete particular entity without roundtrip (If it is a big performance issue in your app).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1657785" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1655729</link><pubDate>Thu, 04 Dec 2008 03:23:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1655729</guid><dc:creator>Terry</dc:creator><description>&lt;p&gt;smnbss - That code could get you into trouble if your entities IQueryable is built from joining two tables. &amp;nbsp;See a more reliable way of deleting at &lt;a rel="nofollow" target="_new" href="http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx"&gt;www.aneyfamily.com/.../Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx&lt;/a&gt;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1655729" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1654455</link><pubDate>Tue, 18 Nov 2008 14:32:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654455</guid><dc:creator>smnbss</dc:creator><description>&lt;p&gt;Add this to your datacontext and you&amp;#39;ll be able to delete in a much more performant way&lt;/p&gt;
&lt;p&gt;like:&lt;/p&gt;
&lt;p&gt;dataContext.Delete(dataContext.Threads.Where(x=&amp;gt;x.ThreadId == threadId));&lt;/p&gt;
&lt;p&gt;public int Delete&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; entities)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			System.Data.Common.DbCommand command = this.GetCommand(entities.AsQueryable());&lt;/p&gt;
&lt;p&gt;			string s = this.GetCommand(entities.AsQueryable()).CommandText;&lt;/p&gt;
&lt;p&gt;			command.CommandText = &amp;quot;DELETE [t0]\r\n&amp;quot; + s.Substring(s.IndexOf(&amp;quot;FROM&amp;quot;));&lt;/p&gt;
&lt;p&gt;			command.Connection.Open();&lt;/p&gt;
&lt;p&gt;			int records = command.ExecuteNonQuery();&lt;/p&gt;
&lt;p&gt;			command.Connection.Close();&lt;/p&gt;
&lt;p&gt;			return records;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;thanks to my colleague Evaldas :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654455" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1654209</link><pubDate>Sat, 15 Nov 2008 18:20:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654209</guid><dc:creator>yaip</dc:creator><description>&lt;p&gt;Are we going back to writing convoluted SQL code by using LINQ? The whole idea was to get away from that. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654209" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653646</link><pubDate>Mon, 10 Nov 2008 10:10:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653646</guid><dc:creator>rajesh</dc:creator><description>&lt;p&gt;Good solution and its very helpfull.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653646" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653119</link><pubDate>Wed, 05 Nov 2008 08:42:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653119</guid><dc:creator>Daniel Fernandes</dc:creator><description>&lt;p&gt;Omar: on a different subject, I downloaded your DropThings project on CodePlex but the database file/scripts are missing.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653119" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653111</link><pubDate>Wed, 05 Nov 2008 05:55:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653111</guid><dc:creator>Terry Aney</dc:creator><description>&lt;p&gt;Great post. &amp;nbsp;Definately a little helper I&amp;#39;ll add to my current list of L2Q extension methods (with a bit of a variation). &amp;nbsp;Anyway, you can see my post at &lt;a rel="nofollow" target="_new" href="http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx"&gt;www.aneyfamily.com/.../Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx&lt;/a&gt; to see how I do batch updates and deletes.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653111" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653074</link><pubDate>Tue, 04 Nov 2008 21:37:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653074</guid><dc:creator>Rafi</dc:creator><description>&lt;p&gt;Can you write similar function for Update? &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653074" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653058</link><pubDate>Tue, 04 Nov 2008 19:45:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653058</guid><dc:creator>Damien Guard</dc:creator><description>&lt;p&gt;This will fail with composite keys but more worryingly is open to SQL injection in the DeleteByPK function where the key is a string.&lt;/p&gt;
&lt;p&gt;You should be very very careful about building SQL out of strings.&lt;/p&gt;
&lt;p&gt;[)amien&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653058" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653056</link><pubDate>Tue, 04 Nov 2008 19:38:07 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653056</guid><dc:creator>cc</dc:creator><description>&lt;p&gt;Not sure I&amp;#39;m a fan... &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653056" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1653049</link><pubDate>Tue, 04 Nov 2008 18:34:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653049</guid><dc:creator>Luke Foust</dc:creator><description>&lt;p&gt;This is great. I have run into this problem so many times but have just dealt with it on a case by case basis. A generic solution is very slick.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653049" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1652824</link><pubDate>Sun, 02 Nov 2008 14:52:13 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652824</guid><dc:creator>Usman Masood</dc:creator><description>&lt;p&gt;No! doing so will simply raise row not found or some thing similar as the data is not matched...&lt;/p&gt;
&lt;p&gt;&amp;quot;Can&amp;#39;t you just attach a constructed object to the datacontext and then call DeleteOnSubmit.&lt;/p&gt;
&lt;p&gt;Entity ent = new Entity();&lt;/p&gt;
&lt;p&gt;Entity.pk = 1;&lt;/p&gt;
&lt;p&gt;datacontext.Attach(ent);&lt;/p&gt;
&lt;p&gt;datacontext.Entity.DeleteOnSubmit(ent);&amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652824" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1652480</link><pubDate>Thu, 30 Oct 2008 09:42:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652480</guid><dc:creator>phil hoy</dc:creator><description>&lt;p&gt;Can&amp;#39;t you just attach a constructed object to the datacontext and then call DeleteOnSubmit.&lt;/p&gt;
&lt;p&gt;Entity ent = new Entity();&lt;/p&gt;
&lt;p&gt;Entity.pk = 1;&lt;/p&gt;
&lt;p&gt;datacontext.Attach(ent);&lt;/p&gt;
&lt;p&gt;datacontext.Entity.DeleteOnSubmit(ent);&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652480" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: Delete an entity using Primary Key only</title><link>http://msmvps.com/blogs/omar/archive/2008/10/30/linq-to-sql-delete-an-entity-using-primary-key-only.aspx#1652467</link><pubDate>Thu, 30 Oct 2008 07:41:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652467</guid><dc:creator>Omari</dc:creator><description>&lt;p&gt;I did something similar but as extension method of Table&amp;lt;TEntity&amp;gt;. I think should be mentioned that if DataContext is not readonly then &amp;nbsp;single item query can return cached entity.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652467" width="1" height="1"&gt;</description></item></channel></rss>