Find items from a list which exist in another list

后端 未结 4 360
攒了一身酷
攒了一身酷 2020-12-08 19:13

I have a List

PropA  
{  
    int a;  
    int b;  
}

and another List

         


        
4条回答
  •  心在旅途
    2020-12-08 19:32

    What you want to do is Join the two sequences. LINQ has a Join operator that does exactly that:

    List first;
    List second;
    
    var query = from firstItem in first
        join secondItem in second
        on firstItem.b equals secondItem.b
        select firstItem;
    

    Note that the Join operator in LINQ is also written to perform this operation quite a bit more efficiently than the naive implementations that would do a linear search through the second collection for each item.

提交回复
热议问题