title: C# How to handle large lists of data quickly
body: <p>I have tried to find some articles on my problem, but haven't found anything that is relevant or that makes sense for my application. Here is my issue:</p>
  
  <p>I have two lists of (> 20,000) items. </p>
  
  <p>I need to check each item in each list against every item in the opposite list. </p>
  
  <p>An implementation of something like this:</p>
  
  <pre><code>    foreach(var item1 in List1)
      {
           foreach(var item2 in List2)
           {
                // Check item 1 against item 2. 
                // Check item 2 against item 1.
           }
      }
  </code></pre>
  
  <p>is extremely slow and unusable because of the work done for the checks. </p>
  
  <p>Is there a more efficient way to handle these large lists of items that need a check like this?</p>
  
  <p>Please let me know if there is more information I can provide.
  Thanks for any help/ suggestions. </p>
  
  <p>I am using C# .NET 3.5</p>
  
  <p>EDIT: Let me try and explain the checks in brief way. </p>
  
  <p>item1 and item2 are part of a pathing system. item1 and item2 are connected by N number of other items. I am checking if item1 is connected (valid path) to item2, and item2 is connected to item1. It cannot be assumed that if item1 -> item2, than item2 -> item1. So both checks are necessary. </p>
  
  <p>The database contains the information if and how item1 -> item2 and if/how item2 -> item1.
  Inside the check, there is a named pipe call to a service to do the check. The service does all the path checking and returns if item1 -> item2, etc. </p>