0

このSQLクエリをLinqからラムダに変換しようとしましたが、成功しませんでした。テーブルのidに一致するテーブルの場所にあるレコードしか持っていません。SQLクエリは正常に機能しますが、ラムダは機能しません

SQL クエリ。

  SELECT Document, Place, Record
   FROM RequiredApplicationDocuments LEFT OUTER JOIN Places ON
     RequiredApplicationDocuments.Id = Places.RequiredApplicationDocumentId
       WHERE Places.SecondPlaceId = 4 OR Places.SecondPlaceId IS NULL

ラムダ

Database.RequiredApplicationDocuments.Join(Database.Placess, 
            ra => ra.Id, fa => fa.RequiredApplicationDocumentId, (fa, ra) =>
            new {Places = fa, RequiredApplicationDocument = ra}).DefaultIfEmpty().toList().Select(fa => new Places
                           {
                  FileName = fa.RequiredApplicationDocument.FileName,
                  LoanApplicationId = fa.RequiredApplicationDocument.LoanApplicationId,
                  Name = fa.RequiredApplicationDocument.Name,
                  RequiredApplicationDocument = fa.RequiredApplicationDocument.RequiredApplicationDocument,
                  Id = fa.Places.Id,
                  CreationDate = fa.RequiredApplicationDocument.CreationDate,
                  Contents = fa.RequiredApplicationDocument.Contents,
                  RequiredApplicationDocumentId = fa.RequiredApplicationDocument.RequiredApplicationDocumentId,
                  LoanApplication = fa.RequiredApplicationDocument.LoanApplication,
                  Type = fa.RequiredApplicationDocument.Type 
            }).AsQueryable();
4

1 に答える 1

4

GroupJoin を使用する

これは、LINQ よりも SQL を対象としています

        var res = RequiredApplicationDocuments.GroupJoin(Places,
            p => p.Id,
            d => d.RequiredApplicationDocumentId,
            (d, places) => new
            {
                Document = d,

                Place = places.Where(p => p.SecondPlaceId == 4).FirstOrDefault(),

                // if don't want to exclude documents with "non-4" places only, 
                // remove this and last where clause
                // but this is what your SQL does
                HasNoPlaces = places.Count() == 0 

            }).Where(r => r.HasNoPlaces || r.Place != null);
于 2012-12-13T22:38:30.743 に答える