2010-03-30 71 views
0

我有以下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(第三张表)?我基本上想要将我在本文开头指定的SQL查询(这正是我所需要的)转换为LinqToEntities。

感谢

+0

什么是'profiles.ProfileSettings'和'profiles.PlatformIdentities'的基数? – 2010-03-31 12:41:15

+0

这两个表与他们的父表“Profiles”共享一对多关系。他们之间没有任何基数。 – user206736 2010-03-31 17:04:21

+0

恩,好吧,但是你只是想抓住许多(每个问题中的LINQ)的第一个记录?你想要哪张唱片? – 2010-03-31 18:47:47

回答

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) 
       } 
+0

上面的查询不起作用,因为外键列(ProfileKey)不能作为linq中的属性访问到像linq到sql这样的实体。这将在EF 4.0版中得到修复,该版本将在.NET 4.0中发布,它将在我读过的下个月发布。 – user206736 2010-04-01 01:26:40

+0

您将会收到与此类似的错误消息: - 'DAL.EntityModels.ProfileSetting'不包含'ProfileKey'的定义,也没有接受类型'DAL.EntityModels.ProfileSetting'类型的第一个参数的扩展方法'ProfileKey'可以被发现(添加使用指令或程序集引用) – user206736 2010-04-01 01:30:26

+0

我基于你提供的SQL的联接,这听起来像你已经改变了你的概念模型。 – Nix 2010-04-01 12:29:34