How to intersect two different IEnumerable collections

孤街浪徒 提交于 2019-11-30 14:29:35

The question doesn't really make sense - what would the result type be? Intersections have to be performed on two sequences of the same type. It sounds like you don't so much want an intersection between two sets, as a filter of the first sequence based on possible values of z2. For example:

HashSet<int> validZ2 = new HashSet<int>(listB.Select(x => x.j6));
var filtered = listA.Where(x => validZ2.Contains(x.z2));

Or possibly as Gabe suggests, you want a join. For example:

var query = from a in listA
            join b in listB on a.z2 equals b.j6
            select new { a, b };

That will give you all pairings of values from the two lists which match on z2/j6.

You need to implement a custom equality comparer (see IEqualityComparer<T> interface) to pass it as a second argument to Intersect().

By using the intersect method, you can get common members between the two enumerables, like this example demonstrates:

[Test]
public void TestObjectIntersect()
{
    var a = new List<object> { 1, 2, 3, "test", "test2" };
    var b = new List<object> { 4, 5, 1, "test2" };
    var c = a.Intersect(b);
    Console.WriteLine(String.Join(",", c.Select(x => x.ToString()).ToArray()));
}
Yuriy Rozhovetskiy
class Program
{

    static void Main(string[] args)
    {
        var aList = (from item in Enumerable.Range(1, 10)
                        select new A { Z1 = item, Z2 = item * 2 }).ToList();

        var bList = (from item in Enumerable.Range(10, 100)
                     select new B { J5 = item, J6 = item / 2 }).ToList();

        var intersect = (from a in aList
                         join b in bList
                            on a.Z2 equals b.J6
                         select new { A = a, B = b }).ToList();

        foreach (var item in intersect)
        {
            Console.WriteLine("A:{{{0}}}, B:{{{1}}}", item.A, item.B);
        }
    }
}

public class A
{
    public int Z1 { get; set; }

    public int Z2 { get; set; }

    // other fields and properties

    public override string ToString()
    {
        return string.Format("Z1={0}, Z2={1}", Z1, Z2);
    }
}

public class B
{
    public int J5 { get; set; }

    public int J6 { get; set; }

    // other fields and properties

    public override string ToString()
    {
        return string.Format("J5={0}, J6={1}", J5, J6);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!