如何相交两个不同的 IEnumerable 集合

发布于 2024-10-27 17:53:07 字数 451 浏览 1 评论 0 原文

我认为这个问题以前已经被问过,但我无法得出明确的答案。我试图找到交叉两个完全不同的可枚举集合的最佳方法(或方法)。

class A:

  • int z1
  • int z2
  • int z3
  • string z4

class B:

  • int j5
  • int j6
  • T j7
  • T j8
  • string j9

..我想相交ListList 位于 z2 == j6 上。

这可以做到吗?

i think this question has been asked before but i havent been able to deduce a clear answer. I am trying to find the best way (or a way) to intersect two completely different ienumerable collections.

class A:

  • int z1
  • int z2
  • int z3
  • string z4

class B:

  • int j5
  • int j6
  • T j7
  • T j8
  • string j9

..I want to intersect List<A> with List<B> on z2 == j6.

can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高跟鞋的旋律 2024-11-03 17:53:07

这个问题确实没有意义——结果类型是什么?必须对相同类型的两个序列执行交集。听起来您不太想要两个集合之间的交集,而是根据 z2 的可能值对第一个序列进行过滤。例如:

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

或者可能正如 Gabe 所建议的那样,您想要加入。例如:

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

这将为您提供两个列表中在 z2/j6 上匹配的所有值对。

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.

眼眸 2024-11-03 17:53:07

您需要实现自定义相等比较器(请参阅 IEqualityComparer> 接口),以将其作为第二个参数传递给 Intersect()

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

浅语花开 2024-11-03 17:53:07

通过使用 intersect 方法,您可以获得两个可枚举之间的公共成员,如以下示例所示:

[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()));
}

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()));
}
找回味觉 2024-11-03 17:53:07
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);
    }
}
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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文