如何相交两个不同的IEnumerable集合 [英] How to intersect two different IEnumerable collections

查看:114
本文介绍了如何相交两个不同的IEnumerable集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题已经被问过,但我还没有能演绎出一个明确的答案。我想找到相交两个完全不同的IEnumerable集合的最佳方式(或方法)

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.

A级:


  • INT Z1

  • INT Z2

  • INT Z3

  • 字符串Z4

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

b类:


  • INT J5

  • INT J6

  • T J7

  • T J8

  • 字符串J9

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

..我想相交列表< A> 列表< B> Z2 == J6

可以这样做?

推荐答案

这个问题没有按'吨真的有意义 - 会是什么结果类型是什么?交点必须在相同类型的两个序列进行。这听起来像你没有那么多要两个集合之间的交集,作为基于Z2的可能值第一序列的过滤器。例如:

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

这会给你从这两个列出了在Z2 / J6匹配值的所有配对。

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

这篇关于如何相交两个不同的IEnumerable集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆