1

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 (Dead Link)

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

Image Link (Dead Link)

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 show just like when you use left outer join.

Thanks for any help.

4

2 回答 2

1
from entity in MyContext.EntityType.Include("ChildEntitiesNavigationPropertyName")
select entity;

这将返回 EntityType 的所有实例,以及 ChildEntitiesNavigationPropertyName(如果存在)。对于表格形式,使用匿名类型:

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

对于 1..* 属性:

from entity in MyContext.EntityType.Include("ChildEntitiesNavigationPropertyName")
from child in entity.ChildEntitiesNavigationPropertyName.DefaultIfEmpty()
select new {ParentProperty = entity.ParentProperty, 
            ChildProperty  = child.ChildProperty};
于 2009-02-11T17:00:53.483 回答
0

我很确定您可以从员工中进行选择,然后在 LINQ 中进行左连接,就像这样(我在这台机器上没有 VS):

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

您可能想要选择您想要的列。希望它能给你你想要的结果。

于 2009-02-13T00:10:43.063 回答