2010-05-07 93 views
0

我有三級分層數據。使用下面的聲明我設法顯示兩個 級別的數據。我需要將它擴展到更多層面。三級分層數據-linq

當前層次結構是模塊 - >文件

我需要把它延伸套餐 - >模塊 - >文件

var data = (from m in DataContext.SysModules 
      join d in DataContext.SysDocuments on m.ModuleID equals d.ModuleID into tempDocs 
      from SysDocument in tempDocs.DefaultIfEmpty() 
      group SysDocument by m).ToList(); 

問候 Tassadaque

回答

0

您應該使用DataLoadOptions DataContext上的屬性。

DataLoadOptions dlo = new DataLoadOptions(); 
    //packages are loaded with their modules... 
dlo.LoadWith<SysPackage>(p => p.SysModules); 
    // ... which are loaded with their documents. 
dlo.LoadWith<SysModule>(m => m.SysDocuments); 
myDataContext.LoadOptions = dlo; 
List<SysPackage> result = myDataContext.SysPackages.ToList(); 
1

有時DataLoadOptions路由不好 - 因爲在子集合上可能需要額外的過濾/排序。這是另一種方式去:

var resultList = 
(
    from pack in myDC.SysPackages 
    let mods = 
    (
    from mod in pack.SysModules.Where(mod => mod.ShouldLoad) 
    let docs = mod.SysDocuments.Where(doc => doc.ShouldLoad) 
    select new {Module = mod, Documents = docs.ToList()} 
) 
    select new {Package = pack, Modules = mods.ToList()} 
).ToList(); 
+0

對不起,以便延遲響應... 謝謝你這解決了我的問題。但我仍然很好奇發佈在問題中的查詢可以被擴展與否 – Tassadaque 2010-05-11 08:42:29