1

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);
        }
    }
Vitor Reis
  • 989
  • 4
  • 24
  • 45
  • Can you show what ListOrders() is doing? Sounds like the IQueryable interface is not properly implemented. (I know of know issues with IQueryable - for "foreach" to work, you need an IEnumerable which IQueryable provide/requires). – Les Oct 22 '10 at 14:06
  • there it is.. Could you please say more about the IQueryable needing an IEnumerable? – Vitor Reis Oct 22 '10 at 14:23
  • 1
    Can you show where the datacontext (`db`) comes from? Is it wrapped around the `foreach` loop with a `using`? – Albin Sunnanbo Oct 22 '10 at 14:34

3 Answers3

0

You can do it like this:

IQueryable<Order> ordersList = ListOrders().where(t=>t.Client_Id != string.Empty);
Marko
  • 20,385
  • 13
  • 48
  • 64
0

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
  • 3,984
  • 1
  • 33
  • 49
  • Client_Id is a property from the Order table (db is the DataContext, declared class-level) – Vitor Reis Oct 22 '10 at 14:39
  • is it related to this item at all: http://stackoverflow.com/questions/1388599/periodic-invalidcastexception-and-the-server-failed-to-resume-the-transaction-w And what database are you using? – MattC Oct 22 '10 at 14:50
  • 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 Oct 22 '10 at 14:55
  • 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 Oct 22 '10 at 15:00
0

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 S. Foley
  • 10,190
  • 12
  • 48
  • 66