Find if listA contains any elements not in listB

后端 未结 7 731
春和景丽
春和景丽 2020-11-30 03:25

I have two lists:

List listA     
List listB

How to check using LINQ if in the listA exists an element w

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 04:25

    You can do it in a single line

    var res = listA.Where(n => !listB.Contains(n));
    

    This is not the fastest way to do it: in case listB is relatively long, this should be faster:

    var setB = new HashSet(listB);
    var res = listA.Where(n => !setB.Contains(n));
    

提交回复
热议问题