Having the following:
public List<int> List1 { get; set; }
...
var x = GiveMeObject(); // x.List2 --> each element on list2 has an Id (int).
...
bool containsAtLeastOne = ???
What is the easiest/fastest/shortest way (in linq) to verify if at least 1 element of list1 is in the list2 ?
Thanks
alternative:
IntersectIf your collections are getting large, you should use
Intersectinstead ofContains, sinceIntersectis at least as fast asContains. Depending on your collecions,Containscan get slow quickly.If your collections are quite small (< 1000 elements), this difference would probably not matter.
If you don’t mind a non-LINQ way and some more lines of code, you could use
which will probably be faster than the LINQ approach.