2013-04-09 107 views
0

我隔壁班:的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()方法但它不應該如此。

屬性MainCatIDSubCatIDInteger 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。

任何想法?

+1

我可能會說廢話,但也許你需要在'布爾Equals'和'int GetHashCode'之前'覆蓋'關鍵字? – 2013-04-09 16:10:04

+0

這不能成爲問題。方法簽名與Object方法不同(請注意,這些方法將MapDescModel參數作爲輸入) – Ofir 2013-04-09 16:14:36

+0

@ConradClark - 否,OP正在實現'IEqualityComparer ',而不是覆蓋'object'方法。 – 2013-04-09 16:16:47

回答

1

你已經實現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方法轉換爲覆蓋方法),就可以再次利用IEqualityComparer方法。

+0

我做到了,現在平等的方法返回true,但明顯仍然不工作 - 看到我的更新 – Ofir 2013-04-09 16:39:07

1

如果您需要幫助,請回答所提問題。

重寫布爾等於(對象obj)被調用,它返回什麼?
你的回答未能回答那個非常關鍵的問題。

既然你不執行對象我懷疑那些覆蓋不叫

Implementing the Equals Method

你申請的IEqualityComparer一類(MapsDescModel),並將其應用到集合

IEqualityComparer Interface

我認爲你需要做的(或者意味着要做的)是Override GetHashCode和Equals on the class MainCategories
但你不張貼MainCategories
的定義,我覺得plur(S)有趣的 - 是

public ObservableCollection<MainCategories> MainCategories 

集合的集合?
考慮沒有命名屬性的類名稱的清晰

+0

我沒有重寫MainCategories中的任何東西。請注意,在我的equals方法中,我只比較了整數。 我收到了關於命名的評論 - 你是對的 – Ofir 2013-04-09 16:49:54