左连接不适用于复杂查询

时间:2012-08-10 11:04:08

标签: c# .net linq linq-to-sql left-join

我有非常复杂的linq查询,它适用于内连接,即没有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并[d&GT; F_ AnonymousType351并[d&GT;˚F _AnonymousType35%5bSystem.Nullable1 [System.Int32],System.String,System.Nullable1%5bSystem.DateTime %5D,System.String,System.Nullable`1%5bSystem.Int32%5D%5D%5D“&GT; System.Nullable1 [System.Int32],System.String,System.Nullable1 [System.DateTime的],System.String ,System.Nullable1 [System.Int32]]]   DefaultIfEmpty [&lt;&gt; f__AnonymousType35'方法,此方法不能   翻译成商店表达。

任何人都可以建议问题出在哪里,这真的很有帮助!

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

 from _z in z_temp.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是否为空。但在这种情况下,您需要更改生成select结果的z_temp并将其替换为select new ZRow(a.CommitListID, etc..)。没什么大不了的。