5

我有以下这段代码:

public TimestampedRowStorage GetTimestampedRowStorage(string startTime, string endTime, long trendSettingID, int? period)
    {
        var timestampedList = (from t in dataContext.TrendRecords
                                     where t.TrendSetting_ID == trendSettingID
                                     select t).ToList();

        return new TimestampedRowStorage
        {
            TimestampedDictionary = timestampedList.ToDictionary(m => m.Timestamp,
                m => (from j in dataContext.TrendSignalRecords
                      where j.TrendRecord_ID == m.ID
                      select j).ToDictionary(p => p.TrendSignalSetting.Name,
                p => (double?)p.Value))
        };
    }

但我总是得到以下异常:

已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。

这是堆栈跟踪:

[MySqlException (0x80004005): 已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭。]
MySql.Data.MySqlClient.MySqlCommand.CheckState() +237 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为) + 146
MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior 行为)+47
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为)+10
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior 行为)+443

[EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。]
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +479
System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +736
System.Data。 Objects.ObjectQuery 1.GetResults(Nullable1 forMergeOption) +149
System.Data.Objects.ObjectQuery 1.Load(MergeOption mergeOption) +148 System.Data.Objects.DataClasses.RelatedEnd.Load() +37 System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad () +8032198 System.Data.Objects.DataClasses.EntityReference 1 来源,Func1.Execute(MergeOption mergeOption) +31
System.Data.Objects.DataClasses.EntityReference

1.get_Value() +12 Nebula.Models.TrendSignalRecord.get_TrendSignalSetting() in C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Models\SmgerDataModel.Designer.cs:2528 Nebula.Models.Trends.TrendRepository.<GetTimestampedRowStorage>b__b(TrendSignalRecord p) in C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Models\Trends\TrendRepository.cs:229 System.Linq.Enumerable.ToDictionary(IEnumerable2 keySelector, Func2 elementSelector, IEqualityComparer 1 source, Func 2 elementSelector) +54 Nebula.Models.Trends.TrendRepository.b__a(TrendRecord m) 在 C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Models\Trends \TrendRepository.cs:227 System.Linq.Enumerable.ToDictionary(IEnumerable 2 keySelector,Func 1 比较器) +240 System.Linq.Enumerable.ToDictionary(IEnumerable 2 keySelector,Func1 comparer) +226
System.Linq.Enumerable.ToDictionary(IEnumerable
2 keySelector, Func
1 source, Func2 elementSelector, IEqualityComparer
1 source, Func2 elementSelector) +53
Nebula.Models.Trends.TrendRepository.GetTimestampedRowStorage(String startTime, String endTime, Int64 trendSettingID, Nullable
1 期)在 C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Models\Trends\TrendRepository.cs:224 Nebula.Models.Trends.TrendRepository.GetTrendSettingContainer(String startTime, String endTime, Int64 unitID, Int64 plantID, Int64 trendSettingID, GridSortOptions gridSortOptions, Nullable 1 page, Nullable1 recordsPerPage, Nullable 1 period, Int64[] trends, Nullable1 allTrends) 在 C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Models\Trends\TrendRepository.cs:206 Nebula .Controllers.GeneratingUnitController.TrendSettings(Int64 id, Int64 plantID, Int64 trendSettingID, String startTime, String endTime, Nullable 1 page, Nullable1 recordsPerPage, GridSortOptions options, Nullable1 period, Int64[] trends, Nullable1 allTrends) 在 C:\Users\Bruno Leonardo\documents\visual studio 2010\Projects\Nebula\Nebula\Controllers\GeneratingUnitController.cs:148 lambda_method(Closure, ControllerBase, Object[]) +543

你们能帮帮我吗?

4

4 回答 4

10

该错误可能是因为您在访问数据库时尝试访问数据库。您应该尝试将两个 Linq 表达式分开。也许放这样的东西:

var TimestampedList = (from t in dataContext.TrendRecords
                                         where t.TrendSetting_ID == trendSettingID
                                         select t).ToList();
TimestampedDictionary = timestampedList.ToDictionary(m => m.Timestamp,
                    m => (from j in dataContext.TrendSignalRecords
                          where j.TrendRecord_ID == m.ID
                          select j).ToDictionary(p => p.TrendSignalSetting.Name,
                    p => (double?)p.Value))
于 2011-06-07T21:53:36.800 回答
10

在第一个查询中调用 .toList()

于 2013-01-30T12:40:31.150 回答
9

我假设 MySql 连接器不支持 MARS(多个活动结果集)。在这种情况下,您不能这样做:

L2SQuery.ToDictionary(m => m.Timestamp, m => AnotherL2SQuery)

执行此操作后,您将枚举第一个 L2S 查询的结果(=DataReader仍处于打开状态),并且您正在为第一个记录中的每条记录执行第二个 L2S 查询(= 您需要第二个DataReader)。

您必须通过调用单独执行第一个查询ToList,然后迭代结果并构建字典。

于 2011-06-07T21:53:50.157 回答
1

我今天也遇到了这个错误。我将连接用于不止一件事。像这样:

  MySqlConnection conn = new MySqlConnection(....);
  conn.Open();
  // Created a database here
  // Populdate the database there
  conn.Close();

但是当我为每个操作打开和关闭连接时,它起作用了:

  MySqlConnection conn = new MySqlConnection(....);
  conn.Open();
  // Created a database here
  conn.Close();
  conn.Open();
  // Populdate the database there
  conn.Close();
于 2017-04-27T19:20:54.840 回答