IQueryable 中的错误 + foreach
我只是想知道是否存在任何已知问题,或者我是否做错了什么。每次我执行如下例所示的操作时,都会收到错误“服务器无法恢复事务”。
我已经问过有关此错误的另一个问题,但现在我发现它只出现在 foreach
循环中
//Listing orders
IQueryable<Order> ordersList = ListOrders();
foreach (Order order in ordersList)
{
if (order.Client_Id != null) //get the exception here.
{
//some code
}
}
Update:ListOrders() 的代码
public IQueryable<Order> ListOrders()
{
try
{
return from o in db.Orders
where o.Id == this.Id
select o;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
I just want to know if there is any known issue, or if I'm doing something wrong. Everytime I do something like the example below, I get an error "the server failed to resume the transaction".
I had already asked another question about this error, but now I've figured out it only appears in the foreach
loops
//Listing orders
IQueryable<Order> ordersList = ListOrders();
foreach (Order order in ordersList)
{
if (order.Client_Id != null) //get the exception here.
{
//some code
}
}
Update: the code for ListOrders()
public IQueryable<Order> ListOrders()
{
try
{
return from o in db.Orders
where o.Id == this.Id
select o;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Client_Id 是延迟加载属性吗?
那么
if (order.Client_Id != null)
检查实际上是在进行某种数据库操作吗?Is your Client_Id a lazy load property?
So is the
if (order.Client_Id != null)
check actually doing some kind of database operation?如果不知道如何处理数据库上下文,就很难确定,因为如果您没有正确使用上下文,则可能会遇到各种问题。为了验证这一点,您可以从列表顺序方法返回一个 IList,如果返回没有错误并且您的 foreach 按预期工作,那么它很可能与您管理上下文的方式有关。
Without knowing how you are handing your DB Context it is hard to know for sure as if you are not using the context correctly you can have all kind of issues. To verify this, you can return an IList from the list order methods and if this returns with out error and your foreach works as expected then it is most likely related to how you are managing the Context.
你可以这样做:
You can do it like this: