查询的结果不能被枚举多次
我正在使用实体框架 (ef) 并收到以下错误:
“查询的结果不能枚举多次。”。
我有一个包含 ef 数据上下文的存储库类。然后我有一个控制器类(不要与 MVC 控制器混淆),其中包含存储库的实例。到目前为止一切顺利...我在控制器上有一个搜索方法,该方法应该返回一个 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();
}
当我调试代码时,我可以进入 foreach 循环,但随后出现错误:
类型异常 '系统.InvalidOperationException' 发生在 System.Data.Entity.dll 中,但是 未在用户代码中处理
附加信息:结果 查询不能枚举超过 一次。
我的实体使用现有存储过程的函数导入。
// 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);
}
函数 import Search
调用存储的过程来返回 SearchItem
的集合。
我有一种感觉,由于 ef 的某些原因,foreach 循环无法迭代。
I am using the entity framework (ef) and am getting the following error:
"The result of a query cannot be enumerated more than once.".
I have a repository class which contains the ef data context. I then have a controller class (not to be confused with MVC controllers) which contains an instance of the repository. 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.
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:
An exception of type
'System.InvalidOperationException'
occurred in System.Data.Entity.dll but
was not handled in user codeAdditional information: The result of
a query cannot be enumerated more than
once.
My entity uses a function import of an existing stored proc.
// 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
.
I have a feeling that the foreach loop can't iterate because of something with the ef.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试通过调用
ToList()
显式枚举结果。更改
为
Try explicitly enumerating the results by calling
ToList()
.Change
to
尝试将其替换
为
,一切都会正常工作。
Try replacing this
with
and everything will work well.
调用存储过程的有问题的代码:
已修复,使用
ToList()
存储在变量中并重用它:Problematic code calling an stored procedure:
Fixed, store in a variable with
ToList()
and reuse it:如果您遇到这种类型的错误,那么我建议您像往常一样使用列表存储过程数据,然后绑定其他控件,因为我也收到此错误,所以我像这样解决了它
例如:-
尝试这样
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:-
try like this