C# Linq intersect/except with one part of object

后端 未结 7 982
终归单人心
终归单人心 2020-11-29 07:16

I\'ve got a class:

class ThisClass
{
  private string a {get; set;}
  private string b {get; set;}
}

I would like to use the Intersect and

7条回答
  •  無奈伤痛
    2020-11-29 07:55

    I know this is old but couldn't you also just override the Equals & GetHashCode on the class itself?

    class ThisClass
    {
      public string a {get; set;}
      private string b {get; set;}
    
      public override bool Equals(object obj)
      {
        // If you only want to compare on a
        ThisClass that = (ThisClass)obj;
        return string.Equals(a, that.a/* optional: not case sensitive? */);
      }
    
      public override int GetHashCode()
      {
        return a.GetHashCode();
      }
    }
    

提交回复
热议问题