什么是linq存储过程结果错误“查询结果不能多​​次枚举”在foreach循环? [英] what is linq stored procedure result error of "The query results cannot be enumerated more than once" in foreach loop?

查看:78
本文介绍了什么是linq存储过程结果错误“查询结果不能多​​次枚举”在foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

i尝试在linq查询中使用foreach循环但是出现此错误:

查询结果不能多​​次枚举

hi all
i try to use foreach loop on a linq query but up this error:
"The query results cannot be enumerated more than once"

string st = "Adel";
dbDidDataContext db = new dbDidDataContext();
var res = db.spJobmenu(st);
foreach (var item in res)
{
    Console.Write(item.name.ToString());
}



明确适用于:


and it clear works on:

string s = res.First().name.ToString();

推荐答案

无法查询结果集。它是一个ISingleResult< t>这与IQueriable和IEnumerable的工作方式略有不同。



尝试添加ToList()。这将确保SP在您开始迭代之前已返回数据:



The result set cannot be queried. It is an ISingleResult<t> which works a bit differently to IQueriable and IEnumerable.

Try adding ToList(). This will make sure that the SP has returned the data before you start iterating through it:

string st = "Adel";
dbDidDataContext db = new dbDidDataContext();
var res = db.spJobmenu(st).ToList();
foreach (var item in res)
{
    Console.Write(item.name.ToString());
}





.First()方法也会导致获取结果,与ToList相同。这就是为什么你给出的例子工作



希望有所帮助^ _ ^

Andy



the .First() method will also cause the results to be fetched, same as ToList. That is why the example you gave works

Hope that helps ^_^
Andy


这篇关于什么是linq存储过程结果错误“查询结果不能多​​次枚举”在foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆