Three level hierarchical data-linq
I have three level hierarchical data. using the statement below i managed 开发者_JAVA百科to display two level data. I need to extend it to one more level.
Current hierachy is Modules-->Documents
I need to extend it as Packages-->Modules-->Documents
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();
Regards Tassadaque
Sometimes the DataLoadOptions route is no good - because additional filtering/ordering may be needed on the child collections. Here's another way to go:
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();
You should use the DataLoadOptions property on 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();
精彩评论