Find if listA contains any elements not in listB

后端 未结 7 710
春和景丽
春和景丽 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条回答
  •  迷失自我
    2020-11-30 04:08

    List has Contains method that return bool. We can use that method in query.

    List listA = new List();
    List listB = new List();
    listA.AddRange(new int[] { 1,2,3,4,5 });
    listB.AddRange(new int[] { 3,5,6,7,8 });
    
    var v = from x in listA
            where !listB.Contains(x)
            select x;
    
    foreach (int i in v)
        Console.WriteLine(i);
    

提交回复
热议问题