views:

70

answers:

1

I have a couple of tables with similar relationship structure to the standard Order, OrderLine tables.
When creating a data context, it gives the Order class an OrderLines property that should be populated with OrderLine objects for that particular Order object.
Sure, by default it will delay load the stuff in the OrderLine property but that should be fairly transparent right?

Ok, here is the problem I have: I'm getting an empty list when I go MyOrder.OrderLines but when I go myDataContext.OrderLines.Where(line => line.OrderId == 1) I get the right list.

public void B()
{
  var dbContext = new Adis.CA.Repository.Database.CaDataContext(
    "<connectionString>");
  dbContext.Connection.Open();
  dbContext.Transaction = dbContext.Connection.BeginTransaction();
  try
  {
    //!!!Edit: Imortant to note that the order with orderID=1 already exists 
    //!!!in the database

    //just add some new order lines to make sure there are some
    var NewOrderLines = new List<OrderLines>()
    {
      new OrderLine() { OrderID=1, LineID=300 },
      new OrderLine() { OrderID=1, LineID=301 },
      new OrderLine() { OrderID=1, LineID=302 },
      new OrderLine() { OrderID=1, LineID=303 }
    };

    dbContext.OrderLines.InsertAllOnSubmit(NewOrderLines);
    dbContext.SubmitChanges();

    //this will give me the 4 rows I just inserted
    var orderLinesDirect = dbContext.OrderLines
          .Where(orderLine => orderLine.OrderID == 1);

    var order = dbContext.Orders.Where(order => order.OrderID == 1);
    //this will be an empty list
    var orderLinesThroughOrder = order.OrderLines;
  }
  catch (System.Data.SqlClient.SqlException e)
  {
    dbContext.Transaction.Rollback();
    throw;
  }
  finally
  {
    dbContext.Transaction.Rollback();
    dbContext.Dispose();
    dbContext = null;
  }
}

So as far as I can see, I'm not doing anything particularly strange but I would think that orderLinesDirect and orderLinesThroughOrder would give me the same result set.
Can anyone tell me why it doesn't?

+1  A: 

You're just adding OrderLines; not any actual Orders. So the Where on dbContext.Orders returns an empty list.

How you can still find the property OrderLines on order I don't understand, so I may be goofing up here.

[Edit] Could you update the example to show actual types, especially of the order variable? Imo, it shoud be an IQueryable<Order>, but it's strange that you can .OrderLines into that. Try adding a First() or FirstOrDefault() after the Where.

Kurt Schelfthout
Ah yes. a problem with my example. I failed to point out that the order with OrderID=1 already exists
Jero