C# Linq to Entity使用方法语法从左表和右表的0或1返回记录?
我试图从主表返回一条记录,并将其连接到可能返回或不返回记录的辅助表。如果可能的话,我需要使用方法语法来完成这项工作 我现在的问题是C# Linq to Entity使用方法语法从左表和右表的0或1返回记录?,c#,entity-framework,linq,linq-to-entities,C#,Entity Framework,Linq,Linq To Entities,我试图从主表返回一条记录,并将其连接到可能返回或不返回记录的辅助表。如果可能的话,我需要使用方法语法来完成这项工作 我现在的问题是 Organizations.Where(a=>a.OrganizationId==10033) .Join(Addresses, org => org.ClientId, addy => addy.ClientId, (org, addy) => new { org, addy }) .Join(Contac
Organizations.Where(a=>a.OrganizationId==10033)
.Join(Addresses, org => org.ClientId, addy => addy.ClientId, (org, addy) => new { org, addy })
.Join(Contacts, o => o.org.OwnerClientId, c => c.ClientId, (o, c) => new { o, c })
.Select(a => new Establishment
{
Name = a.o.org.Name,
City = a.o.addy.PhysicalAddressCity,
State = a.o.addy.PhysicalAddressState,
Id = Convert.ToInt32(a.o.org.OrganizationId),
AddressLine = a.o.addy.PhysicalAddressLine,
ZipCode = a.o.addy.PhysicalFullPostal,
AssignedInspector = "Jacques Clouseau",
ManagerName = a.c.LastName
}).FirstOrDefault()
如果联系人表中有匹配的记录,则返回正确的信息,但即使没有联系人,我也需要它返回组织和地址
这是否可以使用方法语法
*****补充说明:
忘了提到这是在使用EF Core 3.0您需要使用GroupJoin:
Organizations.Where(a=>a.OrganizationId==10033)
.Join(Addresses, org => org.ClientId, addy => addy.ClientId, (org, addy) => new { org, addy })
.GroupJoin(Contacts, o => o.org.OwnerClientId, c => c.ClientId, (o, contact) => new { o, c = contact.FirstOrDefault() })
.Select(a => new Establishment
{
Name = a.o.org.Name,
City = a.o.addy.PhysicalAddressCity,
State = a.o.addy.PhysicalAddressState,
Id = Convert.ToInt32(a.o.org.OrganizationId),
AddressLine = a.o.addy.PhysicalAddressLine,
ZipCode = a.o.addy.PhysicalFullPostal,
AssignedInspector = "Jacques Clouseau",
ManagerName = a.c.LastName
}).FirstOrDefault()
使用导航属性。不加入。这是一组关于PK和FKs的非常粗糙的旧表。