How to intersect two different IEnumerable collections

前端 未结 4 642
遥遥无期
遥遥无期 2021-01-03 00:46

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 differen

4条回答
  •  离开以前
    2021-01-03 01:33

    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 validZ2 = new HashSet(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.

提交回复
热议问题