What is the best way (on average) to compare two lists with LINQ (query syntax or otherwise)
var A = new [] { 1, 2, 3, ... };
var B = new [] { 4, 1, 5, ... };
bool match =
// Some LINQ expression
such that match will be true when at least one element in the first list (1 in this case) equals an element from the second? I don't need to know how many matches there were, or even which one was matched, just that there was at least one match.
Sure there is.
A.Intersect(B).Any();
Intersect is always useful in the least expected circumstances.
You could use Intersect, but that would calculate all matches, which you don't need. So Any is better, since it will short-circuit:
bool match = A.Any(a => B.Any(b => b == a));
you can also use
A.Any(x => B.Contains(x))
You're looking for a combination of Intersect and Any
bool match = 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