Thursday, March 31, 2011

LINQ to Entities how to return all records from the Parent table?

I am unable to select all the records from the parent table using Linq to Entities.

This is a simple DB design (image below):

Image Link
Table relation

This is the exact output I want using Linq to Entities or Linq to SQL (image below):

Image Link
Sql Results

When I use Linq to Entities or Linq To Sql I can only get the records from the child table that has a foreign key relation. I am not able to get the null values as shown above.

I want to have the null values to show just like when you use ‘left outer join’.

Thanks for any help.

From stackoverflow
  • from entity in MyContext.EntityType.Include("ChildEntitiesNavigationPropertyName")
    select entity;
    

    This returns all instances of EntityType, plus ChildEntitiesNavigationPropertyName when/if it exists. For tabular form use an anonymous type:

    from entity in MyContext.EntityType.Include("ChildEntitiesNavigationPropertyName")
    select new {ParentProperty = entity.ParentProperty, 
                ChildProperty  = entity.ChildEntitiesNavigationPropertyName.ChildProperty};
    

    For a 1..* property:

    from entity in MyContext.EntityType.Include("ChildEntitiesNavigationPropertyName")
    from child in entity.ChildEntitiesNavigationPropertyName.DefaultIfEmpty()
    select new {ParentProperty = entity.ParentProperty, 
                ChildProperty  = child.ChildProperty};
    
    EZ : When I do example it just says childproperty is not defined. All though works From Child in entity.ChildEntitiesNavigationPropertyName, which does not give me the Null records I want.
    Craig Stuntz : You must be using a 1..* property then. In that case, use group by to join. I'll update the example.
    EZ : By the way are you seeing my images in the question? The images show up when I edit the question but cannot see them when view the question.
    Craig Stuntz : No. I'm guessing, because I can't see them.
    EZ : Ok put links to the images, not sure why the images are not showing. Thanks for all your help.
    EZ : Craig created another question more specific. http://stackoverflow.com/questions/544378/linq-to-entities-how-to-return-all-records-from-parent
  • I'm pretty sure you can select from employees and then do a left join in LINQ, something like this (I don't have VS on this machine):

    var results = from e in dbContext.Employees join s in dbContext.Sales on e.EmployeeID equals s.EmployeeID select new { e, s };
    

    You may want to select the columns you just want. Hope it gives you the results you want.

    achinda99 : If there is no answer by the time I get into work tomorrow, I'll work on it. But instead of trying to work from the Sales up to the Employees, you need to join Employees with Sales.
    EZ : The lead table is Employee, just can't beleive that a simple query is to difficult. Thanks for your help.
    achinda99 : I completely forgot! Let me have a look and see now.

0 comments:

Post a Comment