问题
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