dc.Accounts.Where(a => blogs.Select(b => b.AccountID).Distinct()
.Contains(a.AccountID));
foreach (Blog blog in blogs)
{
blog.Account = accounts.Where(a => a.AccountID == blog.AccountID)
.FirstOrDefault();
//this is done to access account outside ObjectContext
blog.account = blog.Account;
}
在此代码中,我在 foreach 循环博客之前获取值,但随后出现错误
There is already an open datareader associated with this command which must be closed first.
最佳答案
这通常是当您有外部查询和内部查询并且外部查询仍在运行时引起的。通常,您可以通过强制外部查询完成来解决此问题;只需添加 ToList()
通常就足够了:
var blogs = dc.Accounts.Where(a => blogs.Select(b => b.AccountID).Distinct()
.Contains(a.AccountID)).ToList(); // <======= here
关于c# - 我正在使用 linq,但收到错误 "There is already an open datareader associated with this command which must be closed first",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7185661/