1. 程式人生 > >Inner join and Left Outer Joins in LINQ with Entity Framework

Inner join and Left Outer Joins in LINQ with Entity Framework

分頁(LINQ) 多表關聯引入外來鍵Include()遇到了一個小問題:查詢表引入的外來鍵對應表中沒有該外來鍵對應的記錄,則在生成的sql中查詢不到

 List<VodFileNodeClone> list = MgtvDbGlobal.DataWhereSortingAndPaging(db.Set<VodFileNode>().Include(m => m.File).Include(m => m.Node), sortExpression, sortDirection, page, rows, condExpr)
                    .Select(p => new VodFileNodeClone
                    {
                                FileId=p.FileId,
                                NodeId=p.NodeId,
                                Node=p.Node,
                                File=p.File,
                                Cdn3Preloaded=p.Cdn3Preloaded
                    })
                    .ToList();

解決方案:DefaultIfEmpty()
List<VodFileNodeClone> list = MgtvDbGlobal.DataWhereSortingAndPaging(db.Set<VodFileNode>().Include(m => m.File).Include(m => m.Node), sortExpression, sortDirection, page, rows, condExpr)<span style="color:#ff0000;">.DefaultIfEmpty()</span>
                    .Select(p => new VodFileNodeClone
                    {
                                FileId=p.FileId,
                                NodeId=p.NodeId,
                                Node=p.Node,
                                File=p.File,
                                Cdn3Preloaded=p.Cdn3Preloaded
                    })
                    .ToList();