Include Statement With Linq Join

c# entity-framework linq

Question

IQueryable<Employee> emps = CreateObjectSet<Employee>()
                                  .Include(u => u.Departments)
                                  .AsQueryable();
IQueryable<Products> prods = CreateObjectSet<Products>().AsQueryable();

ObjectContext's CreateObjectSetMethod is called CreateObjectSet.

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

The issue is that even though I use the include statement in the first line to include departments with the workers, the return values never contain departments, therefore they never have any. kindly provide a suggestion.

Please don't advise me to use a simple include and where clause instead of the join statement as the real query is far more involved and wouldn't be useful in my situation.

Thanks

1
12
2/22/2018 9:12:09 PM

Accepted Answer

Include has a known problem with this. You might read the article found at Embrace 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”);

You may also attempt to repair it by grouping your data and choosing the departments as one of the variables in your type if that option doesn't work for you.

The inclusion will then be "fixed" for you by the EF entity relation fixup process.

15
2/22/2018 7:38:06 PM

Popular Answer

The following code can help you fix this:

var query = from emp in emps
            join prod in prods
            on emp.ProductID equals prod.ProductID
            where emp.EmployeeID == 10
            select employee;
var result = query.Include(u => u.Departments)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow