如何在linq join语句中加载eager?

时间:2011-06-03 03:30:36

标签: c# linq linq-to-entities

下面是linq查询,其中我想在选择员工时检索Team对象。如何通过linq join实现预先加载?如果没有linq join语句,我可以检索Team对象(急切加载)。

Employee x = (from emp in context.Employees.Include("Team")
                     join role in context.Roles on 
                           emp.EmployeeId equals role.EmployeeId 
                     select emp ).Single();

1 个答案:

答案 0 :(得分:2)

您可以为上下文设置DataLoadOptions。这些方面的东西:

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Role>(c => c.EmployeeId);
context.LoadOptions = options;

以下是可能有用的文章:http://blogs.msdn.com/b/adonet/archive/2008/10/07/migrating-from-linq-to-sql-to-entity-framework-eager-loading.aspx