title: Error with the Linq command -There is already an open DataReader associated with this Command which must be closed first
body: <p>the below linq statement : </p>
  
  <pre><code>    List&lt;Column&gt; columns = table.Columns.Cast&lt;Column&gt;().SelectMany(t =&gt; table.Columns.Cast&lt;Column&gt;().Where(col =&gt; col.DataType.Equals(DataType.NVarChar(4000))).Select(col =&gt; col)).ToList(); 
  </code></pre>
  
  <p>is taking about 5 seconds to execute , and for 500+ tables , that is very long time. SO I decided to go for parallel loop. SO that process doesn't wait for the stament to get completed and procedds to next iteration. But getting the error -<strong><em>There is already an open DataReader associated with this Command which must be closed first</em></strong></p>
  
  <p>Below is my code -  </p>
  
  <pre><code>    BackgroundWorker worker = new BackgroundWorker();
                          worker.WorkerReportsProgress = true;
                          worker.WorkerSupportsCancellation = true;
                          worker.DoWork += (_, args) =&gt;
                              {
                                  int count = 1;
                                  foreach (Table table in DestinationTables)
                                  {
                                      Task.Factory.StartNew(() =&gt;
                                          {
                                              Thread thread = new Thread(new ThreadStart(() =&gt;
                                                  {
                                                      List&lt;Column&gt; columns = table.Columns.Cast&lt;Column&gt;().SelectMany(t =&gt; table.Columns.Cast&lt;Column&gt;().Where(col =&gt; col.DataType.Equals(DataType.NVarChar(4000))).Select(col =&gt; col)).ToList();
                                                      if (columns.Count &gt; 0)
                                                          encryptedcolumns.Add(table.ToString(), columns);
                                                      Application.Current.Dispatcher.InvokeAsync(() =&gt;
                                                          {
                                                              worker.ReportProgress(count++);
                                                          });
  
  
                                                  }));
                                              thread.Start();
                                          }).Wait();
  
                                  }
  
                              };
                          worker.ProgressChanged += (_, args) =&gt;
                              {
                                  ProgressBarCurrentValue = args.ProgressPercentage;
                                  NotificationText[1] = "Configuration In Progress. " + Math.Round(Convert.ToDouble(progressBarCurrentValue * 100/progressBarMaximumValue) , 2) + "% Completed";
                              };
                          worker.RunWorkerCompleted += (_, args) =&gt;
                              {
                                  NotificationText[1] = "Execution Started. Please wait";
                                  NotifyPropertyChanged("NotificationText");
                                  ExecuteWorker.RunWorkerAsync();
                                  NotifyPropertyChanged("ExecuteWorker");
                              };
                          worker.RunWorkerAsync();
  </code></pre>
  
  <p>If I add parallel foreach and remove all the three threads(backgroundworker ,taskfactory and innermost Thread) , then the system freezes and I am not geting updates at every iteration.</p>