1

次の表を検討してください。

表: ドキュメント

docID, docNr, docScanTime
10, 99, 2012-08-02
11, 98, 2012-08-02
12, 97, 2012-08-02
13, 96, 2012-08-02
14, 95, 2012-08-02

表: DocumentFlag

ユーザー ID、ドキュメント ID、isDelete、isArchive 41、10、1、
NULL 42、11
、NULL、1

Document テーブルには 5 つの行があり、DocumentFlag テーブルには 2 つの行があります。ドキュメントのリストを取得するために、次の Linq ステートメントを使用しています。

List<Document> docList = new List<Document>();
using (AppDataContext context = data.GetDataContext())
{
  docList = (from d in context.Documents
             join f in context.DocumentFlags on d.docID equals f.docID
             where f.usrID == userID
             select new Document
             {
                 DocID = d.docID,
                 DocNr = d.docNr,
                 ScanTime = d.docScanTime.Value,
                 UserID = f.userID,
                 IsDelete = f.isDelete.Value,
                 IsArchive = f.isArchive.Value,
              }).ToList<Document>();
}

public class Document
{
  public int DocID {get; set;}
  public int DocNr {get; set;}
  public DateTime DocScanTime {get; set;}
  public int UserID {get; set;}
  public byte IsDelete {get; set;}
  public byte IsArchive {get; set;}
}

しかし問題は、DocumentFlag にある 2 つの行しか得られないことです。リスト内の DocumentFlag の情報を含む Document のすべての行を取得したい。DocumentFlag にドキュメントに関する情報が含まれていない場合は、isDelete または isArchive に null を格納できます。

何か案は?

4

2 に答える 2

3

Linq で左外部結合を実行したいとします。私の構文は少しさびていますが、次のようになっていると思います。

  docList = (from d in context.Documents
         join f in context.DocumentFlags on d.docID equals f.docID into ftmp
         where f.usrID == userID
         from f in ftmp.DefaultIfEmpty()
         select new Document
         {
             DocID = d.docID,
             DocNr = d.docNr,
             ScanTime = d.docScanTime.Value,
             UserID = f.userID,
             IsDelete = f.isDelete.Value,
             IsArchive = f.isArchive.Value,
          }).ToList<Document>();

次の記事が役立つ場合があります: https://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/

于 2012-08-02T14:37:38.137 に答える
1

あなたが試みているのは、LINQ の LEFT OUTER JOIN です。

これを試して:

using (AppDataContext context = data.GetDataContext())
{
    docList = (from d in context.Documents
                join f in context.DocumentFlags on d.docID equals f.docID into flg
                where f.usrID == userID 
                from fff in flg.DefaultIfEmpty()
                select new Document
                {
                    DocID = d.docID,
                    DocNr = d.docNr,
                    ScanTime = d.docScanTime.Value,
                    UserID = fff.userID,
                    IsDelete = fff.isDelete.Value,
                    IsArchive = fff.isArchive.Value,
                }).ToList<Document>();
}
于 2012-08-02T14:52:17.320 に答える