尝试遍历 Queryable 时出错
Error when trying to iterate through Queryable
foreach(var name in _cust.Select(s => s.Username).Distinct())
{
var x = _cust.Select(s => s.Username == name); //ERROR HERE
//rest of the code here
}
将抛出 There is already an open DataReader associated with this Command which must be closed first.
异常,但是当我添加 .ToList()
.
时它不会发生
到目前为止,我已经四处搜索,但没有找到满意的答案。所以我的问题是:
- 除了添加
.ToList()
之外,还有其他方法吗?
- 如果我使用
.ToList()
作为解决方案,这会在投入生产时导致性能问题吗?因为 AFAIK ToList()
会尝试加载内存中的所有内容。
- 我的实体中有很多导航属性,
ToList()
会尝试加载导航属性中的所有内容吗?
谢谢
Is there any other way to do this beside adding .ToList()?
我想您的连接字符串中的 enabling MARS 可能是 ToList()
的替代方法。
If I use .ToList() as a solution, will this cause a performance problem when it goes to production?
这取决于您的数据库中不同用户名的数量
Because AFAIK ToList() will try to load everything in the memory.
它只会尝试在内存中加载所有不同的 UserNames
I have a lot of navigation properties in my Entities, will ToList() tried to load everything inside navigation properties?
不,它将执行适当的查询并在 UserName 列上进行投影。
是个好习惯
foreach(var name in _cust.Select(s => s.Username).Distinct())
{
var x = _cust.Select(s => s.Username == name); //ERROR HERE
//rest of the code here
}
将抛出 There is already an open DataReader associated with this Command which must be closed first.
异常,但是当我添加 .ToList()
.
到目前为止,我已经四处搜索,但没有找到满意的答案。所以我的问题是:
- 除了添加
.ToList()
之外,还有其他方法吗? - 如果我使用
.ToList()
作为解决方案,这会在投入生产时导致性能问题吗?因为 AFAIKToList()
会尝试加载内存中的所有内容。 - 我的实体中有很多导航属性,
ToList()
会尝试加载导航属性中的所有内容吗?
谢谢
Is there any other way to do this beside adding .ToList()?
我想您的连接字符串中的 enabling MARS 可能是 ToList()
的替代方法。
If I use .ToList() as a solution, will this cause a performance problem when it goes to production?
这取决于您的数据库中不同用户名的数量
Because AFAIK ToList() will try to load everything in the memory.
它只会尝试在内存中加载所有不同的 UserNames
I have a lot of navigation properties in my Entities, will ToList() tried to load everything inside navigation properties?
不,它将执行适当的查询并在 UserName 列上进行投影。
是个好习惯