<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Overriding GetHashCode / Equals of Entity</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2012 : Overriding GetHashCode / Equals of Entity</description>
  <pubDate>Mon, 13 Apr 2026 14:04:14 -700</pubDate>
  <lastBuildDate>Tue, 20 Aug 2013 12:58:51 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=4284</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Overriding GetHashCode / Equals of Entity : I&amp;#039;m not sure I understand...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=4284&amp;PID=16755#16755</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 4284<br /><strong>Posted:</strong> 20-Aug-2013 at 12:58pm<br /><br />I'm not sure I understand in what situations reference equality wouldn't be appropriate for an entity, unless you're working across EntityManagers.&nbsp; But anyway, GroupBy accepts an IEqualityComparer, so you could implement a simple comparer which uses&nbsp;the EntityKey to determine equality.]]>
   </description>
   <pubDate>Tue, 20 Aug 2013 12:58:51 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=4284&amp;PID=16755#16755</guid>
  </item> 
  <item>
   <title>Overriding GetHashCode / Equals of Entity :   We have some projections that...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=4284&amp;PID=16754#16754</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1059" rel="nofollow">smi-dan</a><br /><strong>Subject:</strong> 4284<br /><strong>Posted:</strong> 20-Aug-2013 at 10:11am<br /><br />We have some projections that return an entity as a property. When grouping on those properties, despite the entity being the same, it will group based on reference and so we end up with two groups for what is the same entity.&nbsp;<div><br></div><div>As a solution I've overridden GetHashCode and Equals on the entity with;</div><div><br></div><div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; public override int GetHashCode()</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; {</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DealerId == 0 ? base.GetHashCode() : DealerId.GetHashCode();</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div><font face="Courier New, Courier, mono"><br></font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; public override bool Equals(object obj)</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; {</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = obj as Dealer;</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x == null)&nbsp;</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</font></div><div><font face="Courier New, Courier, mono"><br></font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (DealerId == 0 &amp;&amp; x.DealerId == 0)&nbsp;</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ReferenceEquals(this, x);</font></div><div><font face="Courier New, Courier, mono"><br></font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DealerId == x.DealerId;</font></div><div><font face="Courier New, Courier, mono">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div></div><div><font face="Courier New, Courier, mono"><br></font></div><div><font size="2">Which works fine, however, is there another solution? As this solution would require I override these methods for all entities in my model.</font></div><span style="font-size:10px"><br /><br />Edited by smi-dan - 20-Aug-2013 at 10:13am</span>]]>
   </description>
   <pubDate>Tue, 20 Aug 2013 10:11:27 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=4284&amp;PID=16754#16754</guid>
  </item> 
 </channel>
</rss>