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
        };

これは結果を返さず、デバッグモードで結果を表示しているときに次の例外メッセージが表示されます。

LINQtoEntitiesはメソッド'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.

より洗練された代替手段は、必要なフィールドを定義して を呼び出すクラスを作成することです。これにより、必要なたびに is null_z.DefaultIfEmpty(new ZRow())かどうかをテストする必要がなくなります。_zただし、この場合select、 の結果を生成するを変更しz_temp、 に置き換える必要がありますselect new ZRow(a.CommitListID, etc..)。大したことではありません。

于 2012-08-10T16:17:45.787 に答える