title: Document / Folder (Parent Child) Display - only show folders with contents
body: <p>I am building my own simple document display model with the concept of Directories (Folders) with containing Documents.  I have built the database and can successfully get a list of all the documents that are in each folder.  My security model has only security on a document level and NOT on a folder level so when i render out the display of folders (and documents) alot of the time many users are seeing empty folders which are not relevant.  What is an efficient way of filtering out the folders that don't contain direct children of documents (except those that have nested folders who DO contain documents)</p>
  
  <p>Below is my recursive function that generates my JSON</p>
  
  <pre><code>        public List&lt;DirectoryViewModel&gt; FlatToHierarchy(List&lt;Data.Directory&gt; list, List&lt;MyDocumentsViewModel&gt; 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 =&gt; x.DirectoryId == i.Id).ToList(),
                      Directories = FlatToHierarchy(list, documents, i.Id)
                  }).ToList();
      }
  </code></pre>
  
  <p>and below is an example of the output generated</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>{  
     "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":[  
  
                 ]
              }
           ]
        }
     ]
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>If anyone could point me in the right direction (either a JS or C# based approach (would prefer backend though) that would be great.</p>
  
  <p>Thank you</p>