左连接时强制内部连接的实体框架DefaultIfEmpty()使用的语法

时间:2015-12-15 22:25:47

标签: c# oracle join linq-to-entities entity-framework-6

我尝试使用.DefaultIfEmpty()语法,使用Oracle EF框架(Oracle.ManagedDataAccess.EntityFramework nuget软件包,版本12.1.2400)创建一个包含大量左连接的相当复杂的查询:

from i in dbHR.Identities

join p in dbHR.Personals
    on new {
        key1 = i.ID,
        key2 = true,
        key3 = true
    } equals new {
        key1 = p.EID_ID,
        key2 = (DbFunctions.TruncateTime(p.EFFECTIVE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0),
        key3 = (DbFunctions.TruncateTime(p.EXPIRY).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0)
    }
    into pj    from p in pj.DefaultIfEmpty()

join s in dbHR.States on p.DSP_ID_ADDRESS equals s.ID into sj from s in sj.DefaultIfEmpty()

join e in dbHR.Employments
    on new {
            key1 = i.ID,
            key2 = true
        }
        equals new {
            key1 = e.EID_ID,
            key2 = (!e.TERMINATION_DATE.HasValue || DbFunctions.TruncateTime(e.TERMINATION_DATE.Value).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0)
        }
    into ej from e in ej.DefaultIfEmpty()

join a in dbHR.Assignments
    on new {
        key1 = e.ID,
        key2 = true
    } equals new {
        key1 = a.EEM_ID,
        key2 = ((!a.ASSIGNMENT_END_DATE.HasValue || DbFunctions.TruncateTime(a.ASSIGNMENT_END_DATE.Value).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0)
            && (DbFunctions.TruncateTime(a.ASSIGNMENT_START_DATE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0)
            && (a.PRIME_ASSIGNMENT != null))
    }
    into aj from a in aj.DefaultIfEmpty()

join ad in dbHR.AssignmentDetails
    on
        new { key1 = a.ID, key2 = true }
    equals
        new
        {
            key1 = ad.EAS_ID,
            key2 = (
                DbFunctions.TruncateTime(ad.EXPIRY).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0
                && DbFunctions.TruncateTime(ad.EFFECTIVE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0
                )
        }
    into adj from ad in adj.DefaultIfEmpty()

join j in dbHR.Jobs 
    on ad.DJB_ID equals j.ID 
    into jj from j in jj.DefaultIfEmpty()

join jd in dbHR.JobDetails 
    on new { 
        key1 = j.ID, key2 = true 
    } equals new {
        key1 = jd.DJB_ID,
        key2 =
            (
            DbFunctions.TruncateTime(jd.EFFECTIVE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0
            && DbFunctions.TruncateTime(jd.EXPIRY).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0
            )
    }
    into jdj from jd in jdj.DefaultIfEmpty()

join d in dbHR.Departments 
    on ad.DDP_ID equals d.ID 
    into dj from d in dj.DefaultIfEmpty()

join dd in dbHR.DepartmentDetails 
    on new { 
        key1 = d.ID, key2 = true 
    } equals new {
        key1 = dd.DDP_ID,
        key2 =
            (
            DbFunctions.TruncateTime(dd.EFFECTIVE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0
            && DbFunctions.TruncateTime(dd.EXPIRY).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0
            )
    } into ddj from dd in ddj.DefaultIfEmpty()

select new HREmployeeRecord
{
    i=i,
    p= p,
    s=s,
    e=e,
    a=a,
    ad=ad,
    j=j,
    jd=jd,
    d=d,
    dd=dd
}

由EF生成的结果SQL在大多数表上都保留了连接,但其中3个继续恢复为内连接:

FROM          "DB"."DB_HR_IDENTITIES" "Extent1"
LEFT OUTER JOIN "DB"."DB_HR_PERSONALS" "Extent2" ON ("Extent1"."ID" = "Extent2"."EID_ID") AND [...datetime comparison removed for brevity...]
LEFT OUTER JOIN "DB"."DB_CM_STATE_PROVINCES" "Extent3" ON "Extent2"."DSP_ID_ADDRESS" = "Extent3"."ID"
LEFT OUTER JOIN "DB"."DB_HR_EMPLOYMENTS" "Extent4" ON ("Extent1"."ID" = "Extent4"."EID_ID") AND [...datetime comparison removed for brevity...]
LEFT OUTER JOIN "DB"."DB_HR_ASSIGNMENTS" "Extent5" ON ("Extent4"."ID" = "Extent5"."EEM_ID") AND [...datetime comparison removed for brevity...]
INNER JOIN "DB"."DB_HR_ASSIGNMENT_DETAILS" "Extent6" ON ("Extent5"."ID" = "Extent6"."EAS_ID") AND ((TRUNC("Extent6"."EXPIRY")) >= (TRUNC(LOCALTIMESTAMP))) AND ((TRUNC("Extent6"."EFFECTIVE")) <= (TRUNC(LOCALTIMESTAMP)))
INNER JOIN "DB"."DB_CM_JOBS" "Extent7" ON "Extent6"."DJB_ID" = "Extent7"."ID"
LEFT OUTER JOIN "DB"."DB_CM_JOB_DETAILS" "Extent8" ON ("Extent7"."ID" = "Extent8"."DJB_ID") AND [...datetime comparison removed for brevity...]
INNER JOIN "DB"."DB_CM_DEPARTMENTS" "Extent9" ON "Extent6"."DDP_ID" = "Extent9"."ID"
LEFT OUTER JOIN "DB"."DB_CM_DEPARTMENT_DETAILS" "Extent10" ON ("Extent9"."ID" = "Extent10"."DDP_ID") AND [...datetime comparison removed for brevity...]

我尝试用以下内容替换某些连接,但不幸的是它会产生相同的输出:

from ad in dbHR.AssignmentDetails.Where(x=>
    a.ID == x.EAS_ID &&
    (
        DbFunctions.TruncateTime(x.EXPIRY).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) >= 0
        && DbFunctions.TruncateTime(x.EFFECTIVE).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Now).Value) <= 0
    )

).DefaultIfEmpty()

我开始拔头发。

read in this answer

  

EF似乎使用INNER JOIN来包含一个必需的和LEFT OUTER JOIN来包含一个可选的导航属性。

虽然这是有道理的,但我认为它不适用于我的AssignmentDetails表,因为它是One(Assignments)to Many(AssignmentDetails)关系的Many(FK)方面,可能不是AssignmentDetails记录。

但是,对于Departments和Jobs表,它们可能是真的,因为它们是外键引用的主键表 - 应该总是有记录。但是,这仍然会破坏结果,因为如果在连接中FK位于由于左连接而为NULL的表上,则由于INNER连接而删除整个行。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我的错误。我在一个名为GetEmployees()的函数中包装此查询,该函数返回了一个iQueryable列表,并对其进行了过滤:

results = HRFunctions.GetEmployees().Where(x => x.ad != null);

因为添加了假定AD不应为null的WHERE子句,所以EntityFramework智能地将LEFT JOIN更改为INNER JOIN。

删除.Where()之后,AssignmentDetails现在是一个左连接。