<p>If I have 2 query sources how do I find ones that are in one that are not in the other?</p>

<p>example of join to find items in both:</p>

<pre><code>var results = from item1 in qs1.Items
   join item2 in qs2 on item1.field1 equals item2.field2
   select item1;
</code></pre>

<p>So what would the linq code be to return the items in qs1 that are not in qs2?</p>

[{'answer_id': 50719, 'author': 'Bramha Ghosh', 'author_id': 3268, 'author_profile': 'https://Stackoverflow.com/users/3268', 'pm_score': 2, 'selected': False, 'text': '<p>From <a href="http://programminglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx" rel="nofollow noreferrer">Marco Russo</a></p>\n\n<pre><code>NorthwindDataContext dc = new NorthwindDataContext();\ndc.Log = Console.Out;\nvar query =\n    from c in dc.Customers\n    where !(from o in dc.Orders\n            select o.CustomerID)\n           .Contains(c.CustomerID)\n    select c;\nforeach (var c in query) Console.WriteLine( c );\n</code></pre>\n'}, {'answer_id': 50724, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>use the Except extension method.</p>\n\n<pre><code>var items1 = new List&lt;string&gt; { "Apple","Orange","Banana" };\nvar items2 = new List&lt;string&gt; { "Grapes","Apple","Kiwi" };\n\nvar excluded = items1.Except(items2);\n</code></pre>\n'}, {'answer_id': 50728, 'author': 'Nidonocu', 'author_id': 483, 'author_profile': 'https://Stackoverflow.com/users/483', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s a more simple version of the same thing, you don\'t need to nest the query:</p>\n\n<pre><code>List&lt;string&gt; items1 = new List&lt;string&gt;();\nitems1.Add("cake");\nitems1.Add("cookie");\nitems1.Add("pizza");\n\nList&lt;string&gt; items2 = new List&lt;string&gt;();\nitems2.Add("pasta");\nitems2.Add("pizza");\n\nvar results = from item in items1\n          where items2.Contains(item)\n          select item;\n\nforeach (var item in results)\n    Console.WriteLine(item); //Prints \'pizza\'\n</code></pre>\n'}, {'answer_id': 50748, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 1, 'selected': False, 'text': '<p>Another totally different way of looking at it would be to pass a lambda expression (condition for populating the second collection) as a predicate to the first collection. </p>\n\n<p>I know this is not the exact answer to the question. I think other users already gave the correct answer.</p>\n'}, {'answer_id': 823467, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 3, 'selected': True, 'text': '<p>Darren Kopp\'s <a href="https://stackoverflow.com/questions/50702/linq-how-do-you-do-a-query-for-items-in-one-query-source-that-are-not-in-anothe/50724#50724">answer</a>:</p>\n\n<pre><code>var excluded = items1.Except(items2);\n</code></pre>\n\n<p>is the best solution from a performance perspective.</p>\n\n<p><em>(NB: This true for at least regular LINQ, perhaps LINQ to SQL changes things as per <a href="http://programminglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx" rel="nofollow noreferrer">Marco Russo\'s blog post</a>.  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).</em></p>\n\n<p>As a quick example try this in <a href="http://www.linqpad.net/" rel="nofollow noreferrer">LINQPad</a>:</p>\n\n<pre><code>void Main()\n{\n   Random rand = new Random();\n   int n = 100000;\n   var randomSeq = Enumerable.Repeat(0, n).Select(i =&gt; rand.Next());\n   var randomFilter = Enumerable.Repeat(0, n).Select(i =&gt; rand.Next());\n\n   /* Method 1: Bramha Ghosh\'s/Marco Russo\'s method */\n   (from el1 in randomSeq where !(from el2 in randomFilter select el2).Contains(el1) select el1).Dump("Result");\n\n   /* Method 2: Darren Kopp\'s method */\n   randomSeq.Except(randomFilter).Dump("Result");\n}\n</code></pre>\n\n<p>Try commenting one of the two methods out at a time and try out the performance for different values of n.</p>\n\n<p>My experience (on my Core 2 Duo Laptop) seems to suggest:</p>\n\n<pre><code>n = 100. Method 1 takes about 0.05 seconds, Method 2 takes about 0.05 seconds\nn = 1,000. Method 1 takes about 0.6 seconds, Method 2 takes about 0.4 seconds\nn = 10,000. Method 1 takes about 2.5 seconds, Method 2 takes about 0.425 seconds\nn = 100,000. Method 1 takes about 20 seconds, Method 2 takes about 0.45 seconds\nn = 1,000,000. Method 1 takes about 3 minutes 25 seconds, Method 2 takes about 1.3 seconds\n</code></pre>\n\n<p><em>Method 2 (Darren Kopp\'s answer) is clearly faster.</em></p>\n\n<p>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).</p>\n\n<p><strong>Conclusion:</strong> Use Darren Kopp\'s answer of LINQ\'s \'Except\' method</p>\n'}]
['https://Stackoverflow.com/questions/50702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1215/']