【问题标题】:The Result of a Query Cannot be Enumerated More than Once (C#) (Entity Framework) (Stored Procedure)不能多次枚举查询的结果 (C#) (Entity Framework) (存储过程)
【发布时间】:2017-11-01 14:53:49
【问题描述】:

当我到达db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList()

我得到以下异常:The Result of a Query Cannot be Enumerated More than Once

我尝试通过调用 ToList() 来更改 LabelComponents,就像建议的 answer 一样。

long? GroupID = db.GetGroupIDByName("PrintTest").SingleOrDefault();
ObjectResult<long?> LabelIDs = db.GetLabelIDSFromGroupingsByGroupID(GroupID.ToString());
for (int i = 0; i < LabelIDs.Count(); i++)
{
    var LabelComponents = db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList();
    List<Component> Label = new List<Component>();

    for(int j = 0; j < LabelComponents.Count(); j++)
    {
        ....
        ....

【问题讨论】:

  • 第一句的代码和你的sn-p不匹配,你确定你的实际代码中有ToList吗?
  • @DavidG var LabelComponents = db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList();它就在那里。没有?
  • .ToList()放在GetLabelIDSFromGroupingsByGroupID()之后
  • 您需要在 first db 调用中使用 ToList,您将多次枚举 LabelIDs 值。

标签: c# sql asp.net-mvc stored-procedures


【解决方案1】:

您需要在 first db 调用中使用 ToList,因为您要多次枚举 LabelIDs 值。 LabelIDs.Count 第一次运行查询,然后LabelIDs.ElementAt 稍后再次运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多