我有下一节课:
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()
方法,但它不能正常工作。
属性MainCatID
和SubCatID
是Integer
s。当我将 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]));' 返回真。
有任何想法吗?