24

我有一堂课:

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

我想使用 Linq 的 Intersect 和 except 方法,即:

private List<ThisClass> foo = new List<ThisClass>();
private List<ThisClass> bar = new List<ThisClass>();

然后我分别填写这两个列表。例如,我想做以下事情(我知道这是不对的,只是伪代码):

foo[a].Intersect(bar[a]);

我该怎么做?

4

7 回答 7

39

如果您想要一个想要相交的单个属性的列表,那么所有其他漂亮的 LINQ 解决方案都可以正常工作。但!如果您想在整个班级上相交并因此有 aList<ThisClass>而不是List<string>您必须编写自己的相等比较器。

foo.Intersect(bar, new YourEqualityComparer());

与 相同Except

public class YourEqualityComparer: IEqualityComparer<ThisClass>
{

    #region IEqualityComparer<ThisClass> Members


    public bool Equals(ThisClass x, ThisClass y)
    {
        //no null check here, you might want to do that, or correct that to compare just one part of your object
        return x.a == y.a && x.b == y.b;
    }


    public int GetHashCode(ThisClass obj)
    {
        unchecked
        {
            var hash = 17;
                            //same here, if you only want to get a hashcode on a, remove the line with b
            hash = hash * 23 + obj.a.GetHashCode();
            hash = hash * 23 + obj.b.GetHashCode();

            return hash;    
        }
    }

    #endregion
}
于 2012-05-17T10:00:27.550 回答
35

也许

// returns list of intersecting property 'a' values
foo.Select(f => f.a).Intersect(bar.Select(b => b.a));

顺便说一句,财产a应该是公开的。

于 2012-05-17T09:54:47.177 回答
5

不确定与相交和比较相比的速度,但如何:

//Intersect
var inter = foo.Where(f => bar.Any(b => b.a == f.a));
//Except - values of foo not in bar
var except = foo.Where(f => !bar.Any(b => b.a == f.a));
于 2015-01-14T18:05:05.013 回答
2
foo.Select(x=>x.a).Intersect(bar.Select(x=>x.a))
于 2012-05-17T09:55:25.933 回答
0

究竟想要的效果是什么?当两个实例通过 的唯一值标识时,您想要获得由a类中所有 ' 组成的字符串列表,还是获得 的列表?ThisClassThisClassa

如果是前者,@lazyberezovksy 和@Tilak 的两个答案应该可以工作。如果是后者,您将不得不重写IEqualityComparer<ThisClass>IEquatable<ThisClass>知道Intersect是什么使两个ThisClass等效实例:

 private class ThisClass : IEquatable<ThisClass>
 {
     private string a;

     public bool Equals(ThisClass other)
     {
        return string.Equals(this.a, other.a);
     }
 }

然后你可以打电话:

 var intersection = foo.Intersect(bar);     
于 2012-05-17T10:02:49.410 回答
0

我知道这是旧的,但你不能也只是覆盖类本身的 Equals 和 GetHashCode 吗?

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();
  }
}
于 2016-11-10T19:44:14.407 回答
-3

您应该创建 IEqualityComparer。您可以将 IEqualityComparer 传递给 Intersect() 方法。这将帮助您更轻松地获得 List(与 bar 相交)。

var intersectionList = foo.Intersect(bar, new ThisClassEqualityComparer()).ToList();


class ThisClassEqualityComparer : IEqualityComparer<ThisClass>
{

    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;
    }

}
于 2012-05-17T10:01:30.470 回答