繁体   English   中英

左联接不适用于复杂查询

[英]Left join is not working for complex query

我有一个非常复杂的linq查询,对于内部z_temp.DefaultIfEmpty()很好,即没有z_temp.DefaultIfEmpty() 但是,当我将其用于左联接时,查询不会产生结果。

var q = from x in db.EmployeesList
        where x.EmployeesListStartDate >= startDate && x.EmployeesListStartDate <= endDate
        join y in db.Survey on x.Survey.SurveyID equals y.SurveyID
        join z in
                (from a in db.Commit
                 join b in
                         (from commit in db.Commit
                          where
                            commit.CommitListID != null &&
                            commit.CommitType.ToUpper() != "PREVIEW"
                          group commit by new
                          {
                              commit.CommitListID
                          } into g
                          select new
                          {
                              CommitListID = (Int32?)g.Key.CommitListID,
                              CommitId = (Int32?)g.Max(p => p.CommitId)
                          })
                       on new { a.CommitListID, a.CommitId }
                   equals new { b.CommitListID, CommitId = (Int32)b.CommitId }
                 select new
                 {
                     CommitListID = (Int32?)a.CommitListID,
                     CommitUsername= a.CommitUsername,
                     CommitStartDateTime=a.CommitStartDateTime,
                     CommitType=a.CommitType,
                     CommitSuccessCount=a.CommitSuccessCount
                 }) on new { EmployeesListID = x.EmployeesListID } equals new { EmployeesListID = (Int32)z.CommitListID }
                 into z_temp
        from _z in z_temp.DefaultIfEmpty()
        select new CustomEmployeesList
        {
            SurveyId = x.Survey.SurveyID != null ? (int)x.Survey.SurveyID : 0,
            EmployeesListId = x.EmployeesListID != null ? (int)x.EmployeesListID : 0,
            EmployeesListName = x.EmployeesListName,
            SpecificMessage = x.SpecificMessage,
            ListCriteria = x.ListCriteria,
            Channel = x.Channel,
            EmployeesListStartDate = (DateTime)x.EmployeesListStartDate,
            EmployeesListEndDate = (DateTime)x.EmployeesListEndDate,
            Records = x.Records != null ? (int)x.Records : 0,
            QueryId = x.AppSqlQueries.QueryId != null ? (int)x.AppSqlQueries.QueryId : 0,
            //AuditId = (Int32?)x.AuditEntry.AuditId,
            StatusCommonCode = x.CommonCode.CommonCodeId != null ? (int)x.CommonCode.CommonCodeId : 0,
            SurveyName = y.SurveyName,
            LastCommitDateTime = _z.CommitStartDateTime.HasValue ? (DateTime)_z.CommitStartDateTime : DateTime.MinValue,
            LastCommitType = _z.CommitType != null ? _z.CommitType : "",
            LastCommitUsername = _z.CommitUsername != null ? _z.CommitUsername : "",
            LastCommitCount = _z.CommitSuccessCount.HasValue ? (int)_z.CommitSuccessCount : 0
        };

这没有返回任何结果,并且在调试模式下查看结果时出现以下异常消息:

LINQ to Entities无法识别方法'System.Collections.Generic.IEnumerable1 [<> f_ AnonymousType351 [<> f _AnonymousType35%5bSystem.Nullable1 [System.Int32],System.String,System.Nullable1%5bSystem.DateTime%5d, System.String,System.Nullable`1%5bSystem.Int32%5d%5d%5d“> System.Nullable1 [System.Int32],System.String,System.Nullable1 [System.DateTime],System.String,System.Nullable1 [System.Int32]] DefaultIfEmpty [<> f__AnonymousType35'方法,并且该方法无法转换为商店表达式。

任何人都可以提出问题的根源,这真的很有帮助!

问题在这一行:

 from _z in z_temp.DefaultIfEmpty()

如果没有行与DefaultIfEmpty()匹配,则调用DefaultIfEmpty()将返回null 好的,您是左联接,但是在访问其成员之前,您必须测试_z是否为null

 ...
 LastCommitDateTime = _z == null ? DateTime.MinValue : (_z.CommitStartDateTime.HasValue ? (DateTime)_z.CommitStartDateTime : DateTime.MinValue),
 LastCommitType = _z == null ? "" : (_z.CommitType != null ? _z.CommitType : ""),
 ...
 etc.

一个更优雅的替代方法是创建一个类,该类定义所需的字段并调用_z.DefaultIfEmpty(new ZRow()) ,因此您无需每次都测试_z是否为null。 但是在这种情况下,您将需要更改为z_temp生成结果的select ,并替换它以select new ZRow(a.CommitListID, etc..) 没有大碍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM