三级分层数据 - linq

时间:2010-05-07 08:03:00

标签: c# asp.net 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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您应该在DataContext上使用DataLoadOptions属性。

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();