passwortsicherheit
passwortsicherheit

Reputation: 35

EntityFramework/LINQ: filter a hierarchical list

I need to filter a hierarchical list like:

Each folder has folders and/or documents. Each document has a Status enum. How can i get all empty Folders or with documents where the status from the document is on "Deleted"?

var folders = from f in context.Folders
              // where f.Documents.All( d => d.Status == DocumentStatus.Deleted )
              select f;

Lazy loading is enabled.

Upvotes: 0

Views: 519

Answers (1)

dutzu
dutzu

Reputation: 3910

How about this:

Folders.Where(folder => folder.Documents.Any(document => document.Status == DocumentStatus.Deleted));

Unfortunately you have a more complex structure, the above linq statement will work only of folders have documents and not other folders.

To do what you want to do you will have to define a Predicate manually and filter with that, because in the case that a folder has children which are folders themselves, you have to recursively call the filter method to see if the condition is true.

Upvotes: 1

Related Questions