I think this would take O(A x B) time to execute.
(where A is the size of collectionA and B is the size of collectionB)
Am I correct?
IEnumerable<A> GetMatches(IEnumerable<A> collectionA, IEnumerable<B> collectionB)
{
foreach (A a in collectionA)
foreach (B b in collectionB)
if (a.Value == b.Value)
yield return a;
}
Is there a faster way to execute this query? (maybe using LINQ?)
Enumerable.Intersectwill not, unfortunately, work as you’re comparing against two separate types (AandB).This will require a bit of handling separately to get an Intersect call that will work.
You could do this in stages:
Note that this will return duplicates if
collectionBcontains duplicates (but not collectionA), so it will have slightly different results than your looping code.If you want unique matches (only one returned), you could change the last line to: