views:

329

answers:

1

In EF, it is possible to write the following line:

orderLine.OrderReference.EntityKey.EntityKeyValues[0].Value

Wich results in the ID of the associated OrderReference.

What would be the solution if I wanted to know the ID's of the orderLines associated with an order?

A: 

The point of the line you show is to get the ID without loading orderLine.Order. But you can't get the IDs of a collection without loading. So just look at the ID property, either directly or from the context.

// from context
var lineIds = (from o in Context.Orders
               where o.Id = someId
               from l in o.Lines
               select l.Id).AsEnumerable();

// from loaded order
if (!order.Lines.IsLoaded) order.Lines.Load();
var lineIds = from l in order.Lines
              select l.Id;
Craig Stuntz
Ah ok, I thought ID's were loaded too. Since they are indeed in different tables, it makes sense afterall :). Thanks
Bertvan