title: Perform operation on each item in a list against every other item in the list with exclusions
body: <p>I have a list of data with no duplicates. For this example I'll say that my list is</p>
  
  <pre><code>List&lt;string&gt; list1 = new List&lt;string&gt;() { "A", "B", "C", "D" };
  </code></pre>
  
  <p>I want to perform my operation on each item in the list against every other item in the list except where I already performed my operation on them (A-B and B-A) or if they are the same (A-A).</p>
  
  <p>EG.</p>
  
  <pre><code>A against B
  A against C
  A against D
  B against C
  B against D
  C against D
  </code></pre>
  
  <p>Now this is fairly simple to do but my list is very large and this process can be quite time consuming. Also with the data I have I don't need to run the operation on matching data or if the operation has already been run</p>
  
  <p>EG.</p>
  
  <pre><code>A against A - Skip
  A against B - Good
  A against C - Good
  A against D - Good
  B against A - Skip (we already did A against B)
  B against B - Skip
  B against C - Good
  B against D - Good
  C against A - Skip
  </code></pre>
  
  <p>and so on.</p>
  
  <p>What I have been looking for (and I don't even know if it exists) is an easy method I can use to do this rather than firing off two loops and doing my operation and saving the results to compare against later.</p>
  
  <p>Looping through the list is <code>O(n*n)</code> but as I don't need to compare over half of the results this is a waste of time as I know that I only need to check <code>O(n*(n/2))</code></p>
  
  <p>The code I'm currently using is as follows</p>
  
  <pre><code>List&lt;string&gt; list1 = new List&lt;string&gt;() { "A", "B", "C", "D" };
  List&lt;string&gt; list2 = new List&lt;string&gt;(list1);
  List&lt;string&gt; listResult = new List&lt;string&gt;();
  
  list2.Reverse();
  
  int i = 0;
  foreach (var a in list1)
  {
      for (int j = 0; j &lt; (list2.Count / 2); j++)
      {
          i++;
          Console.WriteLine("Looped {0} times", i);
  
          // Don't run against ourself
          if (a == list2[j])
              continue;
  
          if (listResult.Count(x =&gt; (x == a + list2[j]) || (x == list2[j] + a)) == 0)
          {
              listResult.Add(a + list2[j]);
  
              // Perform some operation here
              // operation(a, list2[j]);
          }
      }
  }
  </code></pre>
  
  <p>The above code works fine (I would need to adjust the list2.Count / 2 part to account for an odd numbered list).</p>
  
  <p>Is there a better way to do this? A LINQ extension method I've missed? My problem is I don't really know what to Google for.</p>
  
  <p>I wondered if there was a method that would return the list just containing the items I wanted which I would then loop through and perform my operation. Maybe something using <code>.SelectMany()</code></p>