Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

please I need your help with a Linq expression:

I have nested objects with lists, this is how the main object hierarchy looks like (each dash is an atribute of the sub-class):

Folder
-name
-List<Subfolder> Subfolders
                 -name
                 -List<Document> Documents
                                 -name
                                 -key

Having this hierarchy of objects, I have a Document name, and I want to search for it and return its parent folder (Subfolder)

Example:

Folder
    -name: Customer
    -List<Subfolder> Subfolders
                     -name: Personal
                     -List<Document> Documents
                                     -name: Resume
                                     -key : 1

If I said: "Resume", the linq expression should return me: the subfolder "Personal" (the object).

Please help me, because of the two nested lists I'm having troubles, with one it'll be easy.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
255 views
Welcome To Ask or Share your Answers For Others

1 Answer

folders
    .SelectMany(s => s.SubFolders)
    .FirstOrDefault(s => s.Documents.Any(d => d.Name == "Resume"));

I'm shooting from the hip here but I think that should work....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...