我隔壁班:的ObservableCollection <class name> .Distinct()不工作
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。 當我將兩個具有相同屬性值的對象插入集合並使用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);
}
,現在當我執行鮮明方法仍不能正常工作。 我知道我的平等方法工作,因爲行'Console.WriteLine(MapsGrid [0] .Equals(MapsGrid [1]));'返回true。
任何想法?
我可能會說廢話,但也許你需要在'布爾Equals'和'int GetHashCode'之前'覆蓋'關鍵字? – 2013-04-09 16:10:04
這不能成爲問題。方法簽名與Object方法不同(請注意,這些方法將MapDescModel參數作爲輸入) – Ofir 2013-04-09 16:14:36
@ConradClark - 否,OP正在實現'IEqualityComparer',而不是覆蓋'object'方法。 –
2013-04-09 16:16:47