title: LINQ: Compare two lists and count subset
body: <p>I am comparing 2 lists and I need to collect occurrences of a subset (modulesToDelete) from the master list (allModules) ONLY when MORE than one occurrence is found. (allModules contains modulesToDelete). Multiple occurrences of any module in modulesToDelete means those modules are being shared. One occurrence of a module in modulesToDelete means that module is isolated and is safe to delete (it just found itself). I can do this with nested foreach loops but this is as far as I got with a LINQ expression (which doesn't work)collect:</p>
  
  <pre><code>List&lt;Module&gt; modulesToDelete = { A, B, C, K }
  List&lt;string&gt; allModules = {R, A, B, C, K, D, G, T, B, K }  // need to flag B and K
  
  var mods = from mod in modulesToDelete
  where allModules.Any(name =&gt; name.Contains(mod.Name) &amp;&amp; mod.Name.Count() &gt; 1)
  select mod;
  </code></pre>
  
  <p>here is my nested foreach loops which I want to replace with a LINQ expression:</p>
  
  <pre><code>foreach (Module mod in modulesToDelete)
  {
      int count = 0;
      foreach (string modInAllMods in allModules)
      {
          if (modInAllMods == mod.Name)
          {
              count++;
          }
      }
  
      if (count &gt; 1)
      {
          m_moduleMarkedForKeep.Add(mod);
      }
      else if( count == 1)
      {
          // Delete the linked modules
      }
  }
  </code></pre>