Please consider following tables.
Table: Document
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
Table: DocumentFlag
userID, docID, isDelete, isArchive
41, 10, 1, NULL
42, 11, NULL, 1
There are five rows in Document table and two rows in DocumentFlag table. I am using following Linq statement to get a list of documents.
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;}
}
But the problem is that I get only two rows that are in DocumentFlag. I want to get all rows in Document with information in DocumentFlag in the list. If DocumentFlag does not contain information about the document then it can store null in isDelete or isArchive.
Any ideas?
What you’re attempting is a LEFT OUTER JOIN in LINQ.
Try this: