How do I check whether a List contains an element that exists in another List using LINQ in C#? I don't want to use a for/while loop.
So, if List1 has A, B, C and List2 has B, 1, 2, then I would return true.
Any(tag => list. Contains(tag)); Or: var list = new List<string> { ... }; var query = query.
You could use a nested Any() for this check which is available on any Enumerable : bool hasMatch = myStrings. Any(x => parameters. Any(y => y.
if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));
The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.
Try this:
List<string> a = ...
List<string> b = ...
var inComon = a.Intersect(b).Any();
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