I have a DataLayer class (LINQ2SQL, not entity framework) that returns rows from the database as follows:
public ReadOnlyCollection<UserCars> Cars
{
get
{
using (var context = new TransportopiaDataContext())
{
var result = (from car in context.UserCars
where car.UserId == UserId
select car);
return result.ToList().AsReadOnly();
}
}
}
Now, as you know, you have to do the ToList() because the datacontext goes out of scope when you leave the using() clause. The problem is that there are foreign key relationships; while the context is still in scope, you could do:
string foo = car.Manufacturer.Name;
But doing that after context has been disposed causes an error. So my question is:
“Is there some way of doing the ToList() such that foreign key relationships will also be expanded?”
You can specify this in the
DataLoadOptionsbefore executing your query: