重新访问Linq'equals'关键字-它是否比较值和对对象的引用? [英] Linq 'equals' keyword Revisited - Does it compare values and references to objects?

查看:148
本文介绍了重新访问Linq'equals'关键字-它是否比较值和对对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是对链接文本的后续操作,因此标题中有重新讨论.我将其作为一个新问题提出,因为在原始问题下接受的答案和注释都表明联接查询中使用的equals关键字仅引用比较中的值类型.我相信这会引起误解,如下所述.

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.

在幕后,使用默认的相等比较器通过键控查找来比较键.联接是在Enumerable中实现的.联接和键可以是值或引用.

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.

另一个答案中包括一个使用POCO的示例.我只能假定由于在Linq to Sql中对主键使用Joins,然后由于它们通常是简单的值类型,所以这就是产生混淆的地方.

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?

我弄错了吗?

推荐答案

正如您在问题中指出的那样,equals关键字使用 EqualityComparer<T>.Default .因此,它将以与EqualityComparer<T>.Default比较相同的方式进行比较.

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.

如果要按值进行比较,则可以使实现 IEquatable<T>

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

这篇关于重新访问Linq'equals'关键字-它是否比较值和对对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆