I have a list:
var list = new List<string>();
list.Add("Dog");
list.Add("Cat");
list.Add("Bird");
var list2 = new List<string>();
list2.Add("Dog");
list2.Add("Cat"):
if (list.ContainsAny(list2))
{
Console.Write("At least one of the items in List2 exists in list1)"
}
You're looking to see if the "Intersection" of the lists is non-empty:
if(list.Intersect(list2).Any())
DoStuff();
You simply need Enumerable.Intersect as below:
if (list.Intersect(list2).Any())
{
Console.Write("At least one of the items in List2 exists in list1)"
}
This method produces the set intersection of two sequences by using the default equality comparer to compare values. It returns a sequence that contains the elements that form the set intersection of two sequences.The Enumerable.Any() method determines whether a sequence contains any elements.
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