1

我有非常复杂的 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[<> 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' 方法,并且该方法不能翻译成存储表达式。

任何人都可以提出问题所在,这将非常有帮助!

4

1 回答 1

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每次需要时都测试是否为 null。但在这种情况下,您需要将select产生结果的 更改为z_temp并将其替换为select new ZRow(a.CommitListID, etc..). 没有大碍。

于 2012-08-10T16:17:45.787 回答