ObservableCollection<类名>. distinct()不起作用

本文关键字:不起作用 distinct 类名 ObservableCollection | 更新日期: 2023-09-27 18:08:30

我有下一个类:

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()方法,但它不工作,因为它应该。

MainCatIDSubCatID的性质为Integer s。当我将2个具有相同属性值的对象插入到集合并使用Distinct方法时,什么也不会发生。我检查代码(Console.WriteLine(MapsGrid[0].Equals(MapsGrid[1]));)什么比较返回,它返回假,即使属性是相等的。我错在哪里?


更新:

我编辑了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方法时,它仍然不起作用。我知道我的equals方法正在工作,因为行'Console.WriteLine(MapsGrid[0]. equals (MapsGrid[1]));'返回true。

任何想法?

ObservableCollection<类名>. distinct()不起作用

您已经实现了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方法转换为重写)。

如果你需要帮助,就回答问题。

是否重载bool =(对象obj)得到调用,它返回什么?
你的回答没有回答那个非常关键的问题。

因为你没有实现Object,我怀疑那些OverRides没有被调用

实现Equals方法

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

IEqualityComparer接口

我认为你需要做的(或打算做的)是重写GetHashCode和等号上的类MainCategories
但是你没有发布MainCategories的定义
我发现这个符号很有趣,是

public ObservableCollection<MainCategories> MainCategories 

集合的集合?
考虑不要用类的名字来命名属性