LINQ no delayed loading
Is there an easy开发者_如何学运维 way to force linq to execute a join on a foreign key relationship without accessing an arbitrary member of the referenced table?
For example, if I have a Company object that references a TaxBracket
table and there is a relationship and linq association set up between them I want to be able to access Company.TaxBracket
after the data context has been disposed and without having to do var temp = Company.TaxBracket.randomMemberVariableToExecuteQuery
You can use DataLoadOptions
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Company>(c => c.TaxBracket);
using(var context = new DBContext(){LoadOptions = options})
{
//...
}
Use DataLoadOptions.LoadWith
. So, you'd say something like
using(var context = new MyDataContext()) {
DataLoadOptions dataLoadOptions = new DataLoadOptions();
dataLoadOptions.LoadWith<Company>(c => c.TaxBracket);
context.LoadOptions = dataLoadOptions;
// some query
}
精彩评论