title: There is already an open DataReader associated with this Command which must be closed first
body: <p>I have this query and I get the error in this function:</p>
  
  <pre><code>var accounts = from account in context.Accounts
                 from guranteer in account.Gurantors
                 select new AccountsReport
                 {
                     CreditRegistryId = account.CreditRegistryId,
                     AccountNumber = account.AccountNo,
                     DateOpened = account.DateOpened,
                 };
  
   return accounts.AsEnumerable()
                  .Select((account, index) =&gt; new AccountsReport()
                      {
                          RecordNumber = FormattedRowNumber(account, index + 1),
                          CreditRegistryId = account.CreditRegistryId,
                          DateLastUpdated = DateLastUpdated(account.CreditRegistryId, account.AccountNumber),
                          AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber)
                      })
                  .OrderBy(c=&gt;c.FormattedRecordNumber)
                  .ThenByDescending(c =&gt; c.StateChangeDate);
  
  
  public DateTime DateLastUpdated(long creditorRegistryId, string accountNo)
  {
      return (from h in context.AccountHistory
              where h.CreditorRegistryId == creditorRegistryId &amp;&amp; h.AccountNo == accountNo
              select h.LastUpdated).Max();
  }
  </code></pre>
  
  <p>Error is:</p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which must be closed first. </p>
  </blockquote>
  
  <p><strong>Update:</strong></p>
  
  <p>stack trace added:</p>
  
  <pre><code>InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]
     System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) +5008639
     System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +23
     System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +144
     System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87
     System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
     System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
     System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
     System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
     System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +443
  
  [EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.]
     System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +479
     System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +683
     System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +119
     System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() +38
     System.Linq.Enumerable.Single(IEnumerable`1 source) +114
     System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__3(IEnumerable`1 sequence) +4
     System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +29
     System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +91
     System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +69
     System.Linq.Queryable.Max(IQueryable`1 source) +216
     CreditRegistry.Repositories.CreditRegistryRepository.DateLastUpdated(Int64 creditorRegistryId, String accountNo) in D:\Freelance Work\SuperExpert\CreditRegistry\CreditRegistry\Repositories\CreditRegistryRepository.cs:1497
     CreditRegistry.Repositories.CreditRegistryRepository.&lt;AccountDetails&gt;b__88(AccountsReport account, Int32 index) in D:\Freelance Work\SuperExpert\CreditRegistry\CreditRegistry\Repositories\CreditRegistryRepository.cs:1250
     System.Linq.&lt;SelectIterator&gt;d__7`2.MoveNext() +198
     System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
     System.Linq.&lt;GetEnumerator&gt;d__0.MoveNext() +96
  </code></pre>