Entity framework linq query Include() multiple children entities -


It may actually be an element question, but asking a question involves the organization of several children to spread three levels. What is a good way to do (or more)?

I have 4 tables: company , employee , Employee_Car and Employee_Country < / P>

The company has 1: Relationships with the employee.

The employee's 1: m relationship is with both employee employee and employee.

If I want to write a query that returns data from all 4 tables, I am currently writing:

  company company = context. Companies Include in ("Employee Employee_Car"). Include ("employee employee jobs"). First and last (C => CID == Company ID);  

There should be a more elegant way! It generates long-winded and horrible SQLs

I am using EF4 with VS 2010

Replace NameOfContext with the name of your object context.

  Public static class extensions {public static IQueryable & lt; Company & gt; Full Companions (this name is called theOfontext) {Return Reference. Companies Include ("Employee Emporiequard"). Include ("Employee Employee"); } Public stable company CompanyById (this name is named Opporte Reference, Int Company ID) {Return Reference. Companies Include ("Employee Emporia_ Car"). Include ("employee employee jobs") .FirstOrDefault (c => c.id == companyID); }}  

Then your code gets

  company company = reference. Resolutions (Company) FirstOrDefault (c => c.id == companyID); // or if you want more company company = reference. CompanibiID (company id);  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -