I have two lists:
List<int> listA
List<int> listB
How to check using LINQ if in the listA
exists an element wchich deosn't exists in the listB
? I can use the foreach
loop but I'm wondering if I can do this using LINQ
In & Not in operators “in” operator − This operator is used to check whether an element is present in the passed list or not. Returns true if the element is present in the list otherwise returns false.
Using any() along with a generator expression: list1 = ['item1','item2','item3'] list2 = ['item4','item5','item3'] if any(x in list1 for x in list2): print("Duplicates found.") else: print("No duplicates found.")
a_list = [1, 2, 3, 4, 5] # Checks if the variable "a_list" is a list if type(a_list) == list: print("Variable is a list.") else: print("Variable is not a list.") This results in: "Variable is a list."
listA.Except(listB)
will give you all of the items in listA that are not in listB
if (listA.Except(listB).Any())
listA.Any(_ => listB.Contains(_))
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With