【问题标题】:Document / Folder (Parent Child) Display - only show folders with contents文档/文件夹(父子)显示 - 仅显示包含内容的文件夹
【发布时间】:2017-12-06 03:09:36
【问题描述】:

我正在使用包含文档的目录(文件夹)的概念构建我自己的简单文档显示模型。我已经建立了数据库,并且可以成功获取每个文件夹中所有文档的列表。我的安全模型只有文档级别的安全性,而不是文件夹级别的安全性,所以当我渲染文件夹(和文档)的显示时,很多用户会看到不相关的空文件夹。什么是过滤掉不包含文档的直接子级的文件夹的有效方法(除了那些包含文档的嵌套文件夹)

下面是生成 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":[  

               ]
            }
         ]
      }
   ]
}

如果有人能指出我正确的方向(基于 JS 或 C# 的方法(尽管更喜欢后端),那就太好了。

谢谢

【问题讨论】:

    标签: javascript c# algorithm


    【解决方案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
            };
    

    【讨论】:

    • 这是在递归语句中完成还是在之后完成?我需要确保它可以处理 3-4 深的嵌套文件夹,其中包含一个文档,而不会破坏其父文件夹(其中没有直接包含文档)
    • 已编辑以包括检查子目录。
    • 当我实现这个时,我得到一个堆栈溢出异常..看起来它是无限递归的?
    • 虽然......乍一看我看不出它的“那个”有什么不同:/
    • 嗯,是的,我想我可以看到问题...您只想对 i.ParentId == parentId 的记录执行let subDirectories = FlatToHierarchy(list, documents, i.Id)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2015-10-29
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多