Linq加入Include声明

man*_*der 12 c# linq entity-framework

IQueryable<Employee> emps = CreateObjectSet<Employee>()
                                  .Include(u => u.Departments)
                                  .AsQueryable();
IQueryable<Products> prods = CreateObjectSet<Products>().AsQueryable();
Run Code Online (Sandbox Code Playgroud)

CreateObjectSet是ObjectContext的CreateObjectSetMethod

        return (from emp in emps
                join prod in prods
                on emp.ProductID equals prod.ProductID
                where emp.EmployeeID == 10
                select employee).ToList();
Run Code Online (Sandbox Code Playgroud)

问题来自第一行,我使用include语句并包含员工的部门,其中返回值没有部门,因为它们从未包含在内.请提出一些建议.

这只是一个演示查询,实际查询是非常复杂的,所以请不要建议我不应该使用join语句,而是简单的include和where子句,这对我的方案不起作用.

谢谢

Wou*_*ort 15

这是包含的已知问题.您可以查看以下文章Include in EF

> var results =
>         ((from post in ctx.Posts
>         from blog in post.Blogs
>         where blog.Owner.EmailAddress == “alexj@microsoft.com”
>         select post) as ObjectQuery<Post>).Include(“Comments”);
Run Code Online (Sandbox Code Playgroud)

如果该解决方案不适合您,您还可以尝试通过对数据进行分组并选择部门作为类型中的值之一来修复它.

然后,EF实体关系修复机制将为您"修复"包含.

  • 您指定的链接具有非常干净的解决方案.非常感谢 (3认同)