0

我有下一节课:

public class MapsDescModel : NotificationObject, IEqualityComparer<MapsDescModel>
{
    public MapsDescModel(ObservableCollection<MainCategories> mainCategoty)
    {
        MainCategories = mainCategoty;
    }

    private MainCategories _mainCategorySelectedItem;
    public MainCategories MainCategorySelectedItem
    {
        get { return _mainCategorySelectedItem; }
        set
        {
            if (Equals(value, _mainCategorySelectedItem)) return;
            _mainCategorySelectedItem = value;
            RaisePropertyChanged("MainCategorySelectedItem");
            RaisePropertyChanged("SubCategories");
        }
    }

    private SubCategories _subCategorySelectedItem;
    public SubCategories SubCategorySelectedItem
    {
        get { return _subCategorySelectedItem; }
        set
        {
            if (Equals(value, _subCategorySelectedItem)) return;
            _subCategorySelectedItem = value;
            RaisePropertyChanged("SubCategorySelectedItem");
        }
    }

    private ObservableCollection<MainCategories> _mainCategories;
    public ObservableCollection<MainCategories> MainCategories
    {
        get { return _mainCategories; }
        set
        {
            if (Equals(value, _mainCategories)) return;
            _mainCategories = value;
            RaisePropertyChanged("MainCategories");
        }
    }

    public ObservableCollection<SubCategories> SubCategories
    {
        get
        {
            if (MainCategorySelectedItem != null)
                return
                    Infrastructure.Helpers.ExtensionMethods.ToObservableCollection(
                        MainCategorySelectedItem.SubCategory.AsEnumerable());
            else return new ObservableCollection<SubCategories>();
        }
    }


    public bool Equals(MapsDescModel x, MapsDescModel y)
    {
        return
            x.MainCategorySelectedItem.MainCatID == y.MainCategorySelectedItem.MainCatID &&
            x.SubCategorySelectedItem.SubCatID == y.SubCategorySelectedItem.SubCatID;
    }

    public int GetHashCode(MapsDescModel obj)
    {
        if (Object.ReferenceEquals(obj, null)) return 0;

        if (obj.MainCategorySelectedItem == null || obj.SubCategorySelectedItem == null) 
            return 0;
        else  return obj.MainCategorySelectedItem.Category.GetHashCode() + obj.SubCategorySelectedItem.Category.GetHashCode();
    }
}

我在其他班级有下一个收藏:

MapsGrid = new ObservableCollection<MapsDescModel>()

我正在尝试使用MapGrid.Distinct()方法,但它不能正常工作。

属性MainCatIDSubCatIDIntegers。当我将 2 个具有相同属性值的对象插入到集合和使用Distinct方法中时,没有任何反应。我通过代码 ( Console.WriteLine(MapsGrid[0].Equals(MapsGrid[1]));) 检查比较返回的内容,即使属性相等,它也会返回 false。我的错误在哪里?


更新:

我编辑了 MapsDescModel 类:

  public class MapsDescModel : NotificationObject, IEqualityComparer<MapsDescModel>, IEquatable<MapsDescModel>
    {
        public MapsDescModel(ObservableCollection<MainCategories> mainCategoty)
        {
            MainCategories = mainCategoty;

        }



        private MainCategories _mainCategorySelectedItem;
        public MainCategories MainCategorySelectedItem
        {
            get { return _mainCategorySelectedItem; }
            set
            {
                if (Equals(value, _mainCategorySelectedItem)) return;
                _mainCategorySelectedItem = value;
                RaisePropertyChanged("MainCategorySelectedItem");
                RaisePropertyChanged("SubCategories");
            }
        }

        private SubCategories _subCategorySelectedItem;
        public SubCategories SubCategorySelectedItem
        {
            get { return _subCategorySelectedItem; }
            set
            {
                if (Equals(value, _subCategorySelectedItem)) return;
                _subCategorySelectedItem = value;
                RaisePropertyChanged("SubCategorySelectedItem");
            }
        }


        private ObservableCollection<MainCategories> _mainCategories;
        public ObservableCollection<MainCategories> MainCategories
        {
            get { return _mainCategories; }
            set
            {
                if (Equals(value, _mainCategories)) return;
                _mainCategories = value;
                RaisePropertyChanged("MainCategories");
            }
        }

        public ObservableCollection<SubCategories> SubCategories
        {
            get
            {
                if (MainCategorySelectedItem != null)
                    return
                        Infrastructure.Helpers.ExtensionMethods.ToObservableCollection(
                            MainCategorySelectedItem.SubCategory.AsEnumerable());
                else return new ObservableCollection<SubCategories>();
            }
        }


        public bool Equals(MapsDescModel x, MapsDescModel y)
        {
            if (x.MainCategorySelectedItem == null || x.SubCategorySelectedItem == null ||
                y.MainCategorySelectedItem == null || y.SubCategorySelectedItem == null)
                return false;

            return
                x.MainCategorySelectedItem.MainCatID == y.MainCategorySelectedItem.MainCatID &&
                x.SubCategorySelectedItem.SubCatID == y.SubCategorySelectedItem.SubCatID;
        }

        public int GetHashCode(MapsDescModel obj)
        {
            if (Object.ReferenceEquals(obj, null)) return 0;

            if (obj.MainCategorySelectedItem == null || obj.SubCategorySelectedItem == null)
                return 0;
            else return obj.MainCategorySelectedItem.Category.GetHashCode() + obj.SubCategorySelectedItem.Category.GetHashCode();
        }

        public bool Equals(MapsDescModel other)
        {
            return
            this.Equals(this, other);  // use definition from IEqualityComparer<T>
        }

        public override bool Equals(object obj)
        {
            MapsDescModel other = obj as MapsDescModel;
            if (other == null)
                return base.Equals(obj);
            else
                return this.Equals(other);
        }

        public override int GetHashCode()
        {
            MapsDescModel other = this as MapsDescModel;
            if (other == null)
                return base.GetHashCode();
            else
                return this.GetHashCode(other);
        }

现在当我执行 Distinct 方法时它仍然无法正常工作。我知道我的 Equality 方法有效,因为 'Console.WriteLine(MapsGrid[0].Equals(MapsGrid[1]));' 返回真。

有任何想法吗?

4

2 回答 2

1

你已经实现IEqualityComparer<T>了,如果你想将你的类的实例传递给Distinct

var distinctList = MapGrid.Distinct(new MapsDescModel());

然而,一个更简洁的方法是实现IEquatable<T>,因为Distinct如果你不传入一个,默认情况下会使用它IEqualityComparer<T>

public class MapsDescModel : NotificationObject, IEqualityComparer<MapsDescModel>, IEquatable<MapsDescModel>
{
...
    public bool Equals(MapsDescModel y)
    {
        return
            this.Equals(this,y);  // use definition from IEqualityComparer<T>
    }  
}

我还建议覆盖object.Equalsobject.GetHashCode保持一致性,如果您愿意,可以再次利用这些方法(或者如果您决定不再需要它们,IEqualityComparer只需将方法转换为覆盖)。IEqualityComparer

于 2013-04-09T16:16:12.937 回答
1

如果您需要帮助,请回答提出的问题。

是否会调用 override bool Equals(object obj) 并返回什么?
你的回答未能回答这个非常关键的问题。

由于您没有实现 Object 我怀疑那些 OverRides 没有被调用

实现 Equals 方法

您正在将 IEqualityComparer 应用于一个类(MapsDescModel),它适用于一个集合

IEqualityComparer 接口

我认为您需要做(或打算做)是在 MainCategories 类上 OverRide GetHashCode 和 Equals
但是您没有发布 MainCategories 的定义
而且我发现 plur (s) 很有趣 - 是

public ObservableCollection<MainCategories> MainCategories 

一个集合一个集合?
为了清楚起见,考虑不要将属性命名为类的名称

于 2013-04-09T16:18:53.783 回答