ObservableCollection <class name =“”> .Disininct()无法正常工作</class>

时间:2013-04-09 16:06:58

标签: c# .net observablecollection iequalitycomparer

我有下一堂课:

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()方法,但它无法正常工作。

属性MainCatIDSubCatIDInteger。 当我将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]));'返回true。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您已实施IEqualityComparer<T>,如果您想将班级的实例传递给Distinct,那就没问题了:

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

然而,更简洁的方法是实现IEquatable<T>,因为如果您不传递DistinctIEqualityComparer<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方法转换为覆盖,如果您决定你不再需要它了。)

答案 1 :(得分:1)

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

是否覆盖bool Equals(object obj)被调用以及它返回什么?
您的回复未能回答这个非常关键的问题。

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

Implementing the Equals Method

您正在将IEqualityComparer应用于类(MapsDescModel)并且它适用于集合

IEqualityComparer Interface

我认为你需要做的(或者意味着要做的)是OverRide GetHashCode和Equals on the class MainCategories
但是你没有发布MainCategories的定义
而且我觉得有趣的是 -

public ObservableCollection<MainCategories> MainCategories 

集合的集合?
为清晰起见,请考虑不将属性命名为类的名称