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

发布于 2024-08-04 03:23:12 字数 1060 浏览 1 评论 0原文

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

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; }
}

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

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

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

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

当我执行这样的语句时:

role1.equals(role2);

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

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

这似乎应该有效,但事实并非如此。有人对发生的事情有任何想法吗?

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.

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

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.

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-08-11 03:23:12

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

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

盛夏尉蓝 2024-08-11 03:23:12

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

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

例如,永远不会返回 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));

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

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.

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

This for instance, will never return 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文