两个枚举之间的相等性 [英] Equality between two enumerables

查看:437
本文介绍了两个枚举之间的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有完全相同的参考元素的可枚举对象,想知道为什么Equals不正确.

I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true.

作为附带的问题,下面的代码可以比较每个元素,但是必须有一种更优雅的方法

As a side question, the code below to compare each element works, but there must be a more elegant way

var other = (ActivityService) obj;
if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false;
for (int i = 0; i < AllAccounts.Count(); i++) {
    if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) {
        return false;
    }
}
return true;

推荐答案

看看根据数据类型,您可能还需要使用重载的方法接受IEqualityComparer定义自定义比较方法.

Depending on the data type you may also need to use the overloaded method that accepts an IEqualityComparer to define a custom comparison method.

这篇关于两个枚举之间的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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