我正在使用包含Documents的Directories(文件夹)的概念构建我自己的简单文档显示模型。我已经构建了数据库,并且可以成功获取每个文件夹中所有文档的列表。我的安全模型只有文档级别的安全性,而不是文件夹级别的安全性,所以当我渲染出文件夹(和文档)的显示时,很多用户看到的空文件夹不相关。过滤掉不包含文档直接子文件夹的文件夹的有效方法是什么(除了那些包含文档的嵌套文件夹的文件夹除外)
下面是我生成JSON的递归函数
public List<DirectoryViewModel> FlatToHierarchy(List<Data.Directory> list, List<MyDocumentsViewModel> documents, Guid? parentId = null)
{
return (from i in list
where i.ParentId == parentId
select new DirectoryViewModel
{
Id = i.Id,
Description = i.Description,
ParentId = i.ParentId,
Name = i.Name,
Documents = documents.Where(x => x.DirectoryId == i.Id).ToList(),
Directories = FlatToHierarchy(list, documents, i.Id)
}).ToList();
}
及以下是生成的输出的示例
{
"Directories":[
{
"Id":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Name":"Front of House",
"Description":"Checklists \u0026 Evaluations",
"ParentId":null,
"Documents":[
{
"LatestVersion":false,
"Id":"7a580b85-edff-4c18-9ba4-ea893a5b1c11",
"Name":"Welcome Pack",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603479817)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"263.822 KB",
"Url":"",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603479817)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":false,
"Id":"b288a86d-370e-43b2-a938-f817468ccc44",
"Name":"scenarios",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603539867)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"816.909 KB",
"Url":"",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603539867)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":false,
"Id":"67ac3117-309b-40d5-82ca-424de9723d20",
"Name":"Tour",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603617990)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"262.362 KB",
"Url":"",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603617990)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":false,
"Id":"734bf12e-a219-4190-b390-0369901174bb",
"Name":"Evaluation Sheet",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603797893)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"670.168 KB",
"Url":"",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603797893)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":true,
"Id":"64907293-4fde-4f5c-a6f3-640e002c8122",
"Name":"Walk-through",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603503013)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"500.525 KB",
"Url":"https://xxxxx5VtqKA",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603503013)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":true,
"Id":"bcdb12a6-de80-45c3-ac97-0cab879a4b34",
"Name":"staff",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603575103)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"279.536 KB",
"Url":"https://xxxxxaVnDfQ",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603575103)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":true,
"Id":"b1d02b16-a0ba-4100-ada0-ce36ec647f36",
"Name":"Checklist",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603522967)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"244.976 KB",
"Url":"https://xxxxxdSngtA",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603522967)\/",
"ModifiedBy":"Christopher"
},
{
"LatestVersion":true,
"Id":"226ceb41-ff93-4a4a-963b-3afbefe75a57",
"Name":"Cheat Sheet",
"Description":"",
"Extension":"pdf",
"Secured":false,
"Printable":true,
"Downloadable":true,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509603555770)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"8072d20b-c424-465e-87be-7b09ad64a6cc",
"Size":"788.114 KB",
"Url":"https://xxxxxjyqoBQ",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509603555770)\/",
"ModifiedBy":"Christopher"
}
],
"Directories":[
]
},
{
"Id":"e9d28c6b-8e6d-4429-8ea4-eb3dc3bc64eb",
"Name":"Franchise",
"Description":"All things xxx",
"ParentId":null,
"Documents":[
],
"Directories":[
{
"Id":"d0c9eced-9d02-4332-be47-f1ff2e5a7fe6",
"Name":"Fitout",
"Description":"Description",
"ParentId":"e9d28c6b-8e6d-4429-8ea4-eb3dc3bc64eb",
"Documents":[
{
"LatestVersion":true,
"Id":"b0c17fbe-fd15-4343-9032-039e79d461bc",
"Name":"Finding and Fitting out Your xxxx (Deck)",
"Description":"",
"Extension":"pdf",
"Secured":true,
"Printable":false,
"Downloadable":false,
"CoverImage":"",
"Public":false,
"CreatedDate":"\/Date(1509524413437)\/",
"CreatedBy":"Christopher",
"Version":1,
"DirectoryId":"d0c9eced-9d02-4332-be47-f1ff2e5a7fe6",
"Size":"8.395 MB",
"Url":"https://xxxxxPhIf5g",
"VersionNotes":"",
"ModifiedDate":"\/Date(1509524413437)\/",
"ModifiedBy":"Christopher"
}
],
"Directories":[
]
}
]
}
]
}
&#13;
如果有人能指出我正确的方向(基于JS或C#的方法(虽然更喜欢后端),这将是很棒的。
谢谢
答案 0 :(得分:1)
from i in
(from i in list where i.ParentId == parentId select i)
let subDirectories = FlatToHierarchy(list, documents, i.Id)
where (documents.Any(x => x.DirectoryId == i.Id) || subDirectories.Any())
select new
{
...
Directories = subDirectories
};