将LEFT OUTER JOIN转换为Entity Framework

时间:2012-02-29 13:33:31

标签: c#-4.0 entity-framework-4.3

这是我想要转换为EF4.3的SQL查询

        command = database.GetSqlStringCommand(@"
                                select 
                                    H.AUTHENTICATION_ID, 
                                    USERNAME, 
                                    PERMISSIONS,
                                    ORGANIZATION_IDENTIFIER, 
                                    O.ORGANIZATION_ID 
                                from 
                                    AUTHENTICATION H 
                                        left join [AUTHORIZATION] T on H.AUTHENTICATION_ID=T.AUTHENTICATION_ID 
                                        join ORGANIZATION O on O.ORGANIZATION_ID = T.ORGANIZATION_ID
                                order by H.AUTHENTICATION_ID");

这是我能提出的最好的LINQ:

        var query = from h in context.Authentications
            join t in context.Authorizations on h.AuthenticationId equals t.Authentications.AuthenticationId 
            join o in context.Organizations on t.Organizations.OrganizationId equals o.OrganizationId
            orderby
            h.AuthenticationId
            select new
            { AUTHENTICATION_ID = (Int16?)h.AuthenticationId,
                h.Username,
                t.Permissions,
                o.OrganizationIdentifier,
                OrganizationID = (Int16?)o.OrganizationId
            };

我知道我需要将我的第一个连接(在授权和身份验证之间)合并到一起,比方说x并应用DefaultIfEmpty但是无法弄清楚语法。

编辑:图片澄清: Data Model

任何帮助都将受到高度赞赏。问候。

3 个答案:

答案 0 :(得分:40)

Linq中“左连接”的基本语法如下:

from x in table1
join y in table2 on x.id equals y.id into jointable
from z in jointable.DefaultIfEmpty()
select new
{
  x.Field1, 
  x.Field2,
  x.Field3,
  Field4 = z == null ? 0 : z.Field4
};

在你的情况下,我有点困惑,因为你似乎在你的Linq中使用的实体关系与你的SQL隐含的实体关系不一致;这里的关系是零或一,零或多,一对一等?具体来说,你这样做:

from h in context.Authentications
join t in context.Authorizations on h.AuthenticationId equals t.Authentications.AuthenticationId

但是你的SQL意味着“身份验证”是这里的父级,其中包含零或更多“授权”子级,而不是相反,这更像是:

from h in context.Authentications
from t in h.Authorizations.DefaultIfEmpty()

如果您可以更好地了解数据模型以及您希望从中获得哪些数据,我们可以更轻松地解释该查询在Linq中的外观。假设您的关系与SQL隐含的关系相匹配,您应该能够使用以下Linq查询获得所需内容:

var query = from h in context.Authentications
            from t in h.Authorizations.DefaultIfEmpty()
            select new
            {
                h.AuthenticationId,
                h.Username,
                Permissions = t == null ? null : t.Permissions,
                Organizations = t == null ? new EntitySet<Organization>() : t.Organizations
            };

var query2 = from x in query
             from o in x.organizations.DefaultIfEmpty()
             select new
             {
                 AUTHENTICATION_ID = (short?)x.AuthenticationId,
                 x.Username,
                 x.Permissions,
                 OrganizationIdentifier = o == null ? null : o.OrganizationIdentifier,
                 OrganizationID = o == null ? (short?)null : o.OrganizationID 
             };

答案 1 :(得分:0)

给出问题图中存在的外键,这样的事情怎么样?

var query = from a in context.Authentications
            select new
            {
                a.AuthenticationID,
                a.Username,
                a.Authorisations.Permissions ?? false,
                a.Authorisations.Organisations.OrganisationIdentifier ?? 0
                a.Authorisations.Organisations.OrganisationID ?? 0
            };

答案 2 :(得分:-4)

我继续将整个查询移动到数据库上的存储过程。这首先避免使用LINQ和ObjectBuilder来解决问题。