Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Join With Include Statement

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

CreateObjectSet is ObjectContext's CreateObjectSetMethod

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

The problem is from the first line, i use the include statement and include departments with the employees the return values does not have departments with as they are never included. Kindly suggest something.

This is just a demo query, actual query is far complex, so please don't suggest that i should not go with the join statement, but simple include and where clause, that does not serve me ni my scenario.

Thanks

like image 534
manav inder Avatar asked Oct 19 '11 05:10

manav inder


People also ask

Is include same as join in LINQ?

An Included is intended to retain the original object structures and graphs. A Join is needed to project a flattened representation of the object graph or to join types which are not naturally related through the graph (ie. join the customer's city with a shipping facility's city).

What is include () in LINQ?

LINQ Include allows retrieving the related entities to be read from database in same query. By using the Include method we can easily read all related entities from the database in a single query.

What is the use of include in Entity Framework?

The Entity Framework Core (EF) extension method Include provides us the ability to load additional data besides the entities we are querying for. For example: loading products along with their translations.

What is LINQ in C# with example?

LINQ is the basic C#. It is utilized to recover information from various kinds of sources, for example, XML, docs, collections, ADO.Net DataSet, Web Service, MS SQL Server, and different database servers.


2 Answers

This is a known issue with include. You could have a look at the following article Include in EF

var results =
         ((from post in ctx.Posts
         from blog in post.Blogs
         where blog.Owner.EmailAddress == "[email protected]"
         select post) as ObjectQuery<Post>).Include("Comments");

If that solution won't work for you, you can also try to fix it with grouping your data and selecting the departments as one of the values in your type.

The EF entity relation fixup mechanism will then 'fix' the include for you.

like image 88
Wouter de Kort Avatar answered Oct 04 '22 03:10

Wouter de Kort


This can be solved with the following code:

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)
like image 28
Oleg Polezky Avatar answered Oct 04 '22 04:10

Oleg Polezky