简体   繁体   English

一个查询的结果不能被枚举多次

[英]The result of a query cannot be enumerated more than once

I am using the entity framework (ef) and am getting the following error:我正在使用实体框架 (ef) 并收到以下错误:

"The result of a query cannot be enumerated more than once.". “不能多次枚举查询结果。”。

I have a repository class which contains the ef data context.我有一个包含 ef 数据上下文的存储库 class。 I then have a controller class (not to be confused with MVC controllers) which contains an instance of the repository.然后我有一个 controller class(不要与 MVC 控制器混淆),其中包含存储库的一个实例。 So far so good... I have a search method on the controller which is supposed to return an array of RadComboBoxItemData , which is used to populate a Telerik RadComboBox control.到目前为止一切顺利...我在 controller 上有一个搜索方法,它应该返回一个RadComboBoxItemData数组,该数组用于填充 Telerik RadComboBox 控件。

public RadComboBoxItemData[] Search(int id, string searchText)
{
    var query = context.Search(id, searchText);
    List<RadComboBoxItemData> result = new List<RadComboBoxItemData>();
    foreach (var item in query)
    {
        RadComboBoxItemData itemData = new RadComboBoxItemData();
        itemData.Text = ""; // assign some text here..;
        itemData.Value = ""; /*assign some value here..*/
        result.Add(itemData);
    }

    return result.ToArray();
}

When I debug my code, I can get into the foreach loop, but then I get an error saying:当我调试我的代码时,我可以进入 foreach 循环,但随后我收到一条错误消息:

An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code System.Data.Entity.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

Additional information: The result of a query cannot be enumerated more than once.附加信息:不能多次枚举查询结果。

My entity uses a function import of an existing stored proc.我的实体使用现有存储过程的 function 导入。

// EF repository method calling the function imported method on the data context.
public IEnumerable<SearchItem> Search(int id, string searchText)
{
    return this.entityContext.Search(id, searchText);
}

The function import Search calls a stored precedure to return a collection of SearchItem . function import Search调用存储过程以返回SearchItem的集合。

I have a feeling that the foreach loop can't iterate because of something with the ef.我有一种感觉,由于 ef 的原因,foreach 循环无法迭代。

Try explicitly enumerating the results by calling ToList() . 尝试通过调用ToList()显式枚举结果。

Change 更改

foreach (var item in query)

to

foreach (var item in query.ToList())

Try replacing this 尝试替换它

var query = context.Search(id, searchText);

with

var query = context.Search(id, searchText).tolist();

and everything will work well. 一切都会好起来的。

Problematic code calling an stored procedure:调用存储过程的有问题的代码:

var resultSP = db.StoredProcedure(id);

if (resultSP != null)
{
    var count = resultSP.Count();
    
    var list = resultSP.Select(x=>...);
}

Fixed, store in a variable with ToList() and reuse it:已修复,使用ToList()存储在变量中并重用它:

var resultSP = db.StoredProcedure(id);

if (resultSP != null)
{
    var resultSP_List = resultSP.ToList();
    
    var count = resultSP_List.Count();
    
    var list = resultSP_List.Select(x=>...);
}

if you getting this type of error so I suggest you used to stored proc data as usual list then binding the other controls because I also get this error so I solved it like this ex:- 如果你得到这种类型的错误,所以我建议你像往常一样存储proc数据然后绑定其他控件因为我也得到这个错误所以我解决了它像这样:

repeater.DataSource = data.SPBinsReport().Tolist();
repeater.DataBind();

try like this 试试这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM