c# - 不能多次枚举查询的结果 (C#)( Entity Framework )(存储过程)

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

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

我得到以下异常:不能多次枚举查询的结果

我试图通过像这样调用 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++)
    {
        ....
        ....

最佳答案

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

关于c# - 不能多次枚举查询的结果 (C#)( Entity Framework )(存储过程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47057635/

相关文章:

c# - 如何在 Visual Studio C# 中正确公开 List<object> 属性?

mysql - sql join on string = integer 以在任何 RDBMS 上工作

mysql - 搜索某个日期是否在事件中

javascript - 在 Kendo UI Window 控件中解析 JSON

View 组件中的 Javascript

c# - Response.Redirect 剥离 header 引荐来源网址 - 可以将其添加回去吗?

c# - 从文件路径获取字符串而不是路径格式

c# - SQL bigint 哈希匹配 c# int64 哈希

sql - IN 运算符中的模式匹配 (MySQL)

c# - MVC 日期格式和日期选择器