C# Linq intersect/except with one part of object

后端 未结 7 1008
终归单人心
终归单人心 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:58

    You should create IEqualityComparer. You can pass the IEqualityComparer to Intersect() method. This will help you get List(which intersect with bar) easier.

    var intersectionList = foo.Intersect(bar, new ThisClassEqualityComparer()).ToList();
    
    
    class ThisClassEqualityComparer : IEqualityComparer
    {
    
        public bool Equals(ThisClass b1, ThisClass b2)
        {
            return b1.a == b2.a;
        }
    
    
        public int GetHashCode(Box bx)
        {
           // To ignore to compare hashcode, please consider this.
           // I would like to force Equals() to be called
           return 0;
        }
    
    }
    

提交回复
热议问题