tags:

views:

106

answers:

3

Hi

I have three level hierarchical data. using the statement below i managed 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

A: 

When a Document always has one single module and a module always has one single package, you can simply select the relevant documents and access it's module by using the Module property on Document. You can rewrite your query as follows:

var modules = from module in db.SysModules where module.Documents.Count() > 0 select module;

Now you have a collection of modules with atleast one document. And you can access the documents like this: modules.First().Documents. Now it shouldn't be that hard to do the same with Packages.

Steven
Thanks for the reply but i need a grouping through query as it is done in mentioned query
Tassadaque
A: 

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();
David B
A: 

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();
David B
Sorry for late response...Thanks you this solves my problem. but i am still curious that the query posted in the question can be extended or not
Tassadaque