无法在 Silverlight 上的列表中找到项目的索引

发布于 2024-10-13 08:32:18 字数 1696 浏览 4 评论 0原文

我有一个“国家/地区”类型的项目列表,我正在尝试查找列表中特定国家/地区的索引,但 IndexOf() 方法始终返回 -1。

Country 对象如下所示:

    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }

然后,当我尝试使用 IndexOf() 方法时,我会执行以下操作:

var newcountry = new Country
                     {
                         CountryCode = "VE",
                         CountryName = "VENEZUELA"
                     };
        var countries = ListBoxCountries.Items.Cast<Country>().ToList();

        if (countries.IndexOf(newcountry) == -1)
            countries.Add(newcountry);

假设我有一个已填充国家/地区的列表,并且“委内瑞拉”在列表中,则 IndexOf() 方法永远不会找到国家。

编辑:

所以我在这里得到了 ReSharper 的一点帮助,当我告诉他重写 Equals() 方法时,他做了这个:

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Country)) return false;
            return Equals((Country) obj);
        }

        public bool Equals(Country other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.CountryCode, CountryCode) && Equals(other.CountryName, CountryName);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((CountryCode != null ? CountryCode.GetHashCode() : 0)*397) ^ (CountryName != null ? CountryName.GetHashCode() : 0);
            }
        }

这里又出现了一个问题:做这一切只是为了比较两个对象可以吗?

I have a list of items of type Country and I'm trying to find the index of and specific Country on the list but the IndexOf() method always returns -1.

The Country object look like this:

    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }

Then when I try to use the IndexOf() method I do the next:

var newcountry = new Country
                     {
                         CountryCode = "VE",
                         CountryName = "VENEZUELA"
                     };
        var countries = ListBoxCountries.Items.Cast<Country>().ToList();

        if (countries.IndexOf(newcountry) == -1)
            countries.Add(newcountry);

Lets assume I have an already filled list with with the countries and "Venezuela" is on the list, the IndexOf() method never find the country.

EDIT:

So I got a little help from ReSharper here and he made this once I told him to override the Equals() method:

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Country)) return false;
            return Equals((Country) obj);
        }

        public bool Equals(Country other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.CountryCode, CountryCode) && Equals(other.CountryName, CountryName);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((CountryCode != null ? CountryCode.GetHashCode() : 0)*397) ^ (CountryName != null ? CountryName.GetHashCode() : 0);
            }
        }

And here comes another question: It's ok to do all this just to compare two objects?

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

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

发布评论

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

评论(2

信仰 2024-10-20 08:32:20

我怀疑这是由于参考问题造成的。您需要重写 Country 类中的 Equals(); 方法进行检查。

我会使用这样的代码:

public bool Equals(Country other)
{
    return this.CountryName.Equals(other.CountryName);
}

I suspect this is due to a reference issue. You'll need to override the Equals(); method in your Country class to check.

I'd use code like this:

public bool Equals(Country other)
{
    return this.CountryName.Equals(other.CountryName);
}
同尘 2024-10-20 08:32:20

那是因为 IndexOf 使用引用相等来比较对象

您可以使用这个

var newcountry = new Country
                 {
                     CountryCode = "VE",
                     CountryName = "VENEZUELA"
                 };


bool country = ListBoxCountries.Items.Cast<Country>().FirstOrDefault(c=>c.CountryCode == newcountry.CountryCode && c.CountryName == newcountry.CountryName)

if(country == null)
  countries.Add(newcountry);

或者您可以更好地使用 ovverride Equals() 方法来比较对象。

That's because IndexOf uses reference equality for comparing objects

You can use this

var newcountry = new Country
                 {
                     CountryCode = "VE",
                     CountryName = "VENEZUELA"
                 };


bool country = ListBoxCountries.Items.Cast<Country>().FirstOrDefault(c=>c.CountryCode == newcountry.CountryCode && c.CountryName == newcountry.CountryName)

if(country == null)
  countries.Add(newcountry);

Or you can better ovverride Equals() method to compare objects.

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