title: 'Sequence contains more than one element' (InvalidOperationException) thrown by external System.Core call when using Linq2Sql
body: <p>firstly let me explain that I fully understand why this <code>InvalidOperationException</code> has been thrown and that I am simply looking for a way to avoid it from being thrown. There is a call to <code>System.Linq.Enumerable.SingleOrDefault()</code> that I can see in the Visual Studio 2010 Call Stack window. However, the call is in external <code>Linq2Sql</code> code, so I have no access to change it.</p>
  
  <p><img src="https://i.stack.imgur.com/KheOP.png" alt="Call Stack showing (mostly) external code.">
  (Sorry, you may need to zoom in to see this image properly)</p>
  
  <p>The last internal code call before execution goes external is found in the application <code>dbml.designer.cs</code> file which I do have access to, but still cannot edit because it updates automatically and loses custom changes. It is in a (<code>Linq2Sql</code>) property setter for one of the database tables used in the application and it appears that the problem is caused by the call to the <code>_DbAudioTrackContributors.Entity</code> object:</p>
  
  <pre><code>public DbAudioTrackContributor DbAudioTrackContributors
  {
      get
      {
          return this._DbAudioTrackContributors.Entity;
      }
      set
      {
          // This is the last internal line before the Exception 
          DbAudioTrackContributor previousValue = this._DbAudioTrackContributors.Entity;
  
          // Execution never reaches here
          if (((previousValue != value) || 
          (this._DbAudioTrackContributors.HasLoadedOrAssignedValue == false)))
          {
              this.SendPropertyChanging();
              if ((previousValue != null))
              {
                  this._DbAudioTrackContributors.Entity = null;
                  previousValue.DbAudioTrack = null;
              }
              this._DbAudioTrackContributors.Entity = value;
              if ((value != null))
              {
                  value.DbAudioTrack = this;
              }
              this.SendPropertyChanged("DbAudioTrackContributors");
          }
      }
  }
  </code></pre>
  
  <p>If anyone has a way to provide me with the internal code for the <code>EntityRef&lt;TEntity&gt;</code> struct (or even just the <code>Entity</code> property), this could possibly help me to work out what is causing the problem.</p>
  
  <p>The last line of my own code before the <code>InvalidOperationException</code> is thrown is a call to <code>dataContext.SubmitChanges()</code>:</p>
  
  <pre><code>public int UpdateAudioTrack(AudioTrack audioTrack)
  {
      using (TransactionScope transactionScope = new TransactionScope())
      {
          using (MidasDataContext dataContext = DataContext)
          {
              DbAudioTrack dbAudioTrack = dataContext.DbAudioTracks.Where(
                  g =&gt; g.Id == audioTrack.Id).FirstOrDefault();
              if (dbAudioTrack == null) return -1;
              CopyToDbAudioTrack(audioTrack, dbAudioTrack);
              UpdateAudioTrackContributors(dataContext, audioTrack);
  
              // This is the last line of my code before the Exception 
              dataContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
  
              // Execution never reaches here
              transactionScope.Complete();
              return 0;
          }
      }
  }
  </code></pre>
  
  <p>The <code>CopyToDbAudioTrack</code> method simply copies all of the property values from the <code>AudioTrack</code> object to the <code>Linq2Sql</code> generated <code>DbAudioTrack</code> object and the <code>UpdateAudioTrackContributors</code> method is shown below. </p>
  
  <pre><code>private void UpdateAudioTrackContributors(MidasDataContext dataContext, AudioTrack audioTrack)
  {
      DataList&lt;Label&gt; labels = new DataList&lt;Label&gt;(
          audioTrack.Labels.Except(audioTrack.OriginalState.Labels));
      if (labels.Count &gt; 0) AddAudioTrackContributors(dataContext, audioTrack, labels);
      labels = new DataList&lt;Label&gt;(audioTrack.OriginalState.Labels.Except(audioTrack.Labels));
      if (labels.Count &gt; 0) DeleteAudioTrackContributors(dataContext, audioTrack, labels);
  }
  </code></pre>
  
  <p>This method simply finds the <code>Label</code> objects that have changed and either adds or deletes them in the <code>AudioTrackContributors</code> database table. This table seems to be the root of the problem as it disappears if this code is commented out. However, this method correctly selects the objects to add or delete, so I am still confused. The <code>AddAudioTrackContributors</code> method basically calls the code shown below and the <code>DeleteAudioTrackContributors</code> code is shown below that:</p>
  
  <pre><code>List&lt;DbAudioTrackContributor&gt; dbAudioTrackContributors = new List&lt;DbAudioTrackContributor&gt;();
  foreach (T dataListEntry in dataList)
  {
      DbAudioTrackContributor dbAudioTrackContributor = new DbAudioTrackContributor();
      CopyToDbAudioTrackContributor(audioTrack, dataListEntry, dbAudioTrackContributor, contributorType);
      dbAudioTrackContributors.Add(dbAudioTrackContributor);
  }
  dataContext.DbAudioTrackContributors.InsertAllOnSubmit(dbAudioTrackContributors);
  </code></pre>
  
  <p><code>DeleteAudioTrackContributors</code>:</p>
  
  <pre><code>List&lt;DbAudioTrackContributor&gt; dbAudioTrackContributors = new List&lt;DbAudioTrackContributor&gt;();
  foreach (T dataListEntry in dataList)
  {
      DbAudioTrackContributor dbAudioTrackContributor = dataContext.DbAudioTrackContributors.Where(d =&gt; d.DataListId == dataListEntry.Id &amp;&amp; d.AudioTrackId == audioTrack.Id).FirstOrDefault();
      if (dbAudioTrackContributor != null) dbAudioTrackContributors.Add(dbAudioTrackContributor);
  }
  dataContext.DbAudioTrackContributors.DeleteAllOnSubmit(dbAudioTrackContributors);
  </code></pre>
  
  <p>Again, stepping through the code during execution shows that the above code also correctly selects the right objects to delete. As I can't see any problems in my code, I don't know where to look next.</p>
  
  <p>If anyone has any ideas on how to proceed, I would be very glad to hear them. Many thanks in advance.</p>