如何在Entity Framework中实现左外连接

时间:2010-03-30 23:37:16

标签: linq entity-framework linq-to-entities

我有以下SQL查询: -

select distinct * from dbo.Profiles profiles 
left join ProfileSettings pSet on pSet.ProfileKey = profiles.ProfileKey 
left join PlatformIdentities pId on pId.ProfileKey = profiles.Profilekey

我需要将其转换为LinqToEntities表达式。我尝试过以下方法: -

from profiles in _dbContext.ProfileSet
                            let leftOuter = (from pSet in _dbContext.ProfileSettingSet
                                             select new
                                                        {
                                                            pSet.isInternal
                                                        }).FirstOrDefault()
 select new
                                       {
                                           profiles.ProfileKey,
                                           Internal = leftOuter.isInternal,
                                           profiles.FirstName,
                                           profiles.LastName,
                                           profiles.EmailAddress,
                                           profiles.DateCreated,
                                           profiles.LastLoggedIn,                                               
                                       };

上面的查询工作正常,因为我没有考虑第三个表“PlatformIdentities”。单左左外连接与我上面所做的一起工作。如何包含PlatformIdentities(第3个表)?我基本上想要将我在本文开头指定的SQL查询(这正是我所需要的)转换为LinqToEntities。

由于

1 个答案:

答案 0 :(得分:0)

如果您想要选择不同的内容,请告诉我,但真正的联接位于

之下
  from p in _dbContext.ProfileSet
                join ps in _dbContext.ProfileSettings on p.ProfileKey = ps.ProfileKey  into a
                join pi in _dbContext.PlatformIdentities on p.ProfileKey = pi.ProfileKey  into b

                select new
                {
                   profiles.ProfileKey,
                   profiles.FirstName,
                   profiles.LastName,
                   profiles.EmailAddress,
                   profiles.DateCreated,
                   profiles.LastLoggedIn, 
                   PlatformSettings = a.Select(x=>x),
                   PlatformIdentities = b.Select(y=>y)
                }