If I have 2 query sources how do I find ones that are in one that are not in the other?
example of join to find items in both:
var results = from item1 in qs1.Items
join item2 in qs2 on item1.field1 equals item2.field2
select item1;
So what would the linq code be to return the items in qs1 that are not in qs2?
Darren Kopp’s answer:
is the best solution from a performance perspective.
(NB: This true for at least regular LINQ, perhaps LINQ to SQL changes things as per Marco Russo’s blog post. However, I’d imagine that in the “worst case” Darren Kopp’s method will return at least the speed of Russo’s method even in a LINQ to SQL environment).
As a quick example try this in LINQPad:
Try commenting one of the two methods out at a time and try out the performance for different values of n.
My experience (on my Core 2 Duo Laptop) seems to suggest:
Method 2 (Darren Kopp’s answer) is clearly faster.
The speed decrease for Method 2 for larger n is most likely due to the creation of the random data (feel free to put in a DateTime diff to confirm this) whereas Method 1 clearly has algorithmic complexity issues (and just by looking you can see it is at least O(N^2) as for each number in the first collection it is comparing against the entire second collection).
Conclusion: Use Darren Kopp’s answer of LINQ’s ‘Except’ method