Error in IQueryable + foreach
I just want to know if there is a开发者_JAVA百科ny 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);
}
}
Is your Client_Id a lazy load property?
So is the if (order.Client_Id != null)
check actually doing some kind of database operation?
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.
public IList<Order> ListOrders()
{
try
{
return (from o in db.Orders
where o.Id == this.Id
select o).ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
You can do it like this:
IQueryable<Order> ordersList = ListOrders().where(t=>t.Client_Id != string.Empty);
精彩评论