2013-04-09 94 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

相关问题