我现在用的是实体框架(EF)和正在以下错误列举:查询的结果不能超过一次
"The result of a query cannot be enumerated more than once.".
我有一个包含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循环,但后来我得到一个错误说:
An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: The result of a query cannot be enumerated more than once.
我的实体使用现有的存储过程的函数导入。
// 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);
}
功能导入Search
调用存储precedure返回的SearchItem
的集合。
我有一种感觉,foreach循环无法迭代,因为与ef的东西。
你错过了一个.ToList(),我只是看不到在哪里...我会抓一杯咖啡,再看一看 – Smudge202 2011-04-19 22:45:24