Linq 'equals' keyword Revisited - Does it compare values and references to objects?

痞子三分冷 提交于 2019-12-24 01:53:29

问题


This question is a follow on to link text hence the revisited in the title. I raise this as a new question as the accepted answer and comment made under the original question both suggest that the equals keyword used in the join query refers to only value types within the comparison. I believe this is misleading as follows.

Behind the scenes the default equality comparer is used to compare keys using a keyed lookup. The join is implemented in Enumerable.Join and the key can be either a value or a reference.

Included in the other answer was an example using POCOs. I can only assume that as Joins are used in Linq to Sql on primary keys then as these are often simple value types this is where the confusion has arisen.

So does the equals compare values and references to objects?

Have I got this wrong?


回答1:


As you seem to state in your question, the equals keyword uses EqualityComparer<T>.Default. Therefore, it will compare the same way that EqualityComparer<T>.Default compares.

If you want to compare by value, you can make implement IEquatable<T>



来源:https://stackoverflow.com/questions/1936103/linq-equals-keyword-revisited-does-it-compare-values-and-references-to-objec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!