title: Issue with IEnumerable, Where, and Object.ReferenceEquals
body: <p>Trying to solve an Exercism problem (Dominoes). In this problem I am attempting to create a valid chain of dominoes such that numbers are touching each other and the first and last number are the same.</p>
  
  <p>Example: <code>[1|2], [3,1], [3,2] -&gt; [1|2][2|3][3|1]</code></p>
  
  <p>I am using brute force to try all combinations. To do so I start a chain with all possible combinations using the following code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  public static class Dominoes
  {
      private class Domino
      {
          public readonly int First;
          public readonly int Last;
  
          public Domino(int first, int last)
          {
              First = first;
              Last = last;
          }
  
          public Domino Flip() =&gt; new Domino(Last, First);
      }
  
      private class Chain
      {
          private readonly List&lt;Domino&gt; chained = new List&lt;Domino&gt;();
  
          private readonly List&lt;Domino&gt; remaining = new List&lt;Domino&gt;();
  
          private int First =&gt; chained[0].First;
  
          private int Last =&gt; chained[chained.Count - 1].Last;
  
          public bool Complete =&gt; remaining.Count == 0 &amp;&amp; First == Last;
  
          private Chain(Domino domino, IEnumerable&lt;Domino&gt; remaining, IEnumerable&lt;Domino&gt; chained = null)
          {
              if (chained != null)
              {
                  this.chained.AddRange(chained);
              }
  
              this.chained.Add(domino);
  
              this.remaining.AddRange(remaining);
          }
  
          public static IEnumerable&lt;Chain&gt; Start(IEnumerable&lt;Domino&gt; dominoes)
          {
              var chains = new List&lt;Chain&gt;();
  
              foreach (var domino in dominoes)
              {
                  var remaining = dominoes.Where(d =&gt; d != domino);
  
                  chains.Add(new Chain(domino, remaining));
                  chains.Add(new Chain(domino.Flip(), remaining));
              }
  
              return chains;
          }
  
          public IEnumerable&lt;Chain&gt; Extend()
          {
              var chains = new List&lt;Chain&gt;();
  
              foreach (var domino in this.remaining)
              {
                  var remaining = this.remaining.Where(d =&gt; d != domino);
  
                  if (domino.First == Last)
                  {
                      chains.Add(new Chain(domino, remaining, chained));
                  }
  
                  if (domino.Last == Last)
                  {
                      chains.Add(new Chain(domino.Flip(), remaining, chained));
                  }
              }
  
              return chains;
          }
      }
  
      public static bool CanChain(IEnumerable&lt;(int, int)&gt; dominoes)
      {
          var chains = Chain.Start(dominoes.Select(d =&gt; new Domino(d.Item1, d.Item2)));
  
          return chains.Any() ? Iterate(chains) : true;
      }
  
      private static bool Iterate(IEnumerable&lt;Chain&gt; chains)
      {
          var newChains = new List&lt;Chain&gt;();
  
          foreach (var chain in chains)
          {
              if (chain.Complete) return true;
  
              newChains.AddRange(chain.Extend());
          }
  
          if (newChains.Count == 0) return false;
  
          return Iterate(newChains);
      }
  }
  </code></pre>
  
  <p>Test Code</p>
  
  <pre><code>using System;
  using Xunit;
  
  public class DominoesTests
  {
      [Fact]
      public void Singleton_that_cant_be_chained()
      {
          var dominoes = new[] { (1, 2) };
          Assert.False(Dominoes.CanChain(dominoes));
      }
  }
  </code></pre>
  
  <p>Run using <code>dotnet test</code></p>
  
  <p>The filtering for <code>remaining</code> in <code>Chain.Start</code> does not appear to work.</p>
  
  <p>Example (using <code>new [] { (1, 2) }</code> as input for <code>CanChain</code>) </p>
  
  <pre><code># Expected: `Chain: [1|2] Remaining:`
  # Actual:   `Chain: [1|2] Remaining: [1|2]`
  </code></pre>
  
  <p>If I call <code>.ToList()</code> on the <code>dominoes.Select(d =&gt; new Domino(d))</code> in <code>CanChain</code> it starts working.</p>
  
  <p>EDIT: added more information</p>