Entity Framework Join vs Include

c# entity-framework entity-framework-6 join

Question

I'm using EF6.1 and am a little perplexed by the two join query writing types.

var query = (from cc in dbContext.Companies.AsNoTracking()
             join dc in dbContext.Departments.AsNoTracking() 
             on cc.CompanyId equals dc.CompanyId
                select new {cc,dc}).ToList().Select(k=> New Company()
                              {
                                 CompanyId = k.cc.CompanyId,
                                 Departments = k.dc.ToList()
                              });

vs

var query = dbContext.Comapanies.Include(k=>k.Departments).ToList();

Which one should be used, and what are the benefits and drawbacks?

On using Include when there are several tables, I came across conflicting opinions.

According to article https://msdn.microsoft.com/en-in/data/jj574232.aspx below

"Note that it is not currently possible to filter which related entities are loaded. Include will always being in all related entities."

This assertion is also not very clear. Please provide recommendations.

1
3
2/26/2015 6:10:39 PM

Popular Answer

"Note that the linked entities that are loaded cannot presently be filtered. Include will constantly be present in all associated entities."

This means that 'Includes' will function if the database has properly specified relationships between the tables, while a join statement does not require any such relationships to be defined.

The major consideration is whether to utilize includes or joins; nonetheless, includes are preferable because they are already controlled in the database and in the entity itself.

2
2/26/2015 6:46:39 PM


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