私は次のクラスを持っています:
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
です。同じプロパティ値を持つ 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 メソッドを実行すると、まだ機能しません。「Console.WriteLine(MapsGrid[0].Equals(MapsGrid[1]));」という行があるため、Equality メソッドが機能していることはわかっています。true を返します。
何か案は?