views:

67

answers:

2

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);
        }
    }
A: 

Is your Client_Id a lazy load property?

So is the if (order.Client_Id != null) check actually doing some kind of database operation?

MattC
Client_Id is a property from the Order table (db is the DataContext, declared class-level)
Vitor Reis
is it related to this item at all: http://stackoverflow.com/questions/1388599/periodic-invalidcastexception-and-the-server-failed-to-resume-the-transaction-wAnd what database are you using?
MattC
I'm using SQL Sv2008, .NEt3.5 and the problem is the same: "Periodic" error. It appears that sql server isn't "killing" the query processes, but i'm pretty sure that the problem is the foreach.
Vitor Reis
sounds similar to this issue http://stackoverflow.com/questions/2360751/can-i-force-linq-to-sql-to-use-sql2005provider which refers to this bug submit https://connect.microsoft.com/VisualStudio/feedback/details/366011/linq-to-sql-query-translator-produces-syntactically-incorrect-t-sql-from-datetime-date-method
MattC
A: 

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);
    }
}
Rodney Foley