使用 Nhibernate 时,检查 C#.Net 中的列表相等性不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1401554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:06:51  来源:igfitidea点击:

Checking of List equality in C# .Net not working when using Nhibernate

c#.netnhibernateilist

提问by

I seem to be having a problem with checking for list equality. In my case, I have two role objects and I want to see if they are equal. Each role contains a name and a List of permissions. Each permission contains just a name.

我似乎在检查列表相等性时遇到问题。就我而言,我有两个角色对象,我想看看它们是否相等。每个角色都包含一个名称和一个权限列表。每个权限只包含一个名称。

public class Role : BaseDomain
{
        virtual public String Name { get; set; }
        virtual public IList Permissions { get; set; }
}

public class Permission
{
        virtual public String Name { get; set; }
}

I defined an equals method on both the Role and the Permission objects. These objects are loaded from the database using Nhibernate. This means that the Role actually contains all the Permissions in an object of type NHibernate.Collection.PersistentBag that implements the IList interface.

我在 Role 和 Permission 对象上都定义了一个 equals 方法。这些对象是使用 Nhibernate 从数据库加载的。这意味着角色实际上包含了实现 IList 接口的 NHibernate.Collection.PersistentBag 类型的对象中的所有权限。

In the equals method of the Role class I have a condition as follows:

在 Role 类的 equals 方法中,我有一个条件如下:

if (!IList.Equals(Permissions, otherObj.Permissions)) return false;

This is always returning false. Even when the Permissions in the list contain objects with identical names in identical order. This would make sense if I hadn't implemented an equals method for the Permission object, but I have.

这总是返回false。即使列表中的权限包含以相同顺序具有相同名称的对象。如果我没有为 Permission 对象实现 equals 方法,这将是有意义的,但我已经实现了。

When I execute a statement like this:

当我执行这样的语句时:

role1.equals(role2);

What happens is that it first goes to the Role object's equals method. Good. The equals method checks the name of the role to see if they're equal. They are. Then checks to see if the Permissions are equals using the code given above.

发生的事情是它首先转到 Role 对象的 equals 方法。好的。equals 方法检查角色的名称以查看它们是否相等。他们是。然后使用上面给出的代码检查权限是否相等。

I have a breakpoint in the equals method of the Permission class as well as the GetHashCode method. When that statement is executed, neither the equals method nor the GetHashCode method on the permission class is called but it always returns false. In fact, I can't seen to figure out what happens in order to determine that the two lists are not equal. What gets executed? I can't even step into that line of code.

我在 Permission 类的 equals 方法和 GetHashCode 方法中有一个断点。执行该语句时,权限类上的 equals 方法和 GetHashCode 方法都不会被调用,但它始终返回 false。事实上,我看不出为了确定两个列表不相等会发生什么。什么被执行?我什至无法进入那行代码。

This seems like it should work but it doesn't. Anyone have any ideas on whats going on?

这似乎应该起作用,但实际上不起作用。任何人都对发生了什么有任何想法?

采纳答案by Frederik Gheysels

When you compare 2 lists with each other, the Equals method will NOT compare the items that are in that list. It will compare the List object with the other List object.

当您相互比较 2 个列表时,Equals 方法不会比较该列表中的项目。它将 List 对象与另一个 List 对象进行比较。

A List is an object, which has its own 'identity'.

List 是一个对象,它有自己的“身份”。

This for instance, will never return true:

例如,这永远不会返回 true:

List<int> firstList = new List<int>();
List<int> secondList = new List<int>();

firstList.Add(1);
firstList.Add(2);

secondList.Add(1);
secondList.Add(2);


Assert.IsTrue (firstList.Equals(secondList));

The Equals method does not compare the contents of the list, but the list itself. If you want this behaviour, I think that you'll have to implement your own type of List.

Equals 方法不比较列表的内容,而是比较列表本身。如果您想要这种行为,我认为您必须实现自己的列表类型。

回答by Piotr Czapla

As Fredrik said Equalsdoesn't compare the content of the lists. But Enumerable.SequenceEqualdoes. Check Is there a built-in method to compare collections in C#?for more info.

正如 Fredrik 所说Equals,不会比较列表的内容。但Enumerable.SequenceEqual确实如此。检查 是否有内置方法来比较 C# 中的集合?了解更多信息。