LINQ的嵌套列表表达

LINQ的嵌套列表表达

本文介绍了LINQ的嵌套列表表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我需要一个LINQ表达你的帮助:



我已经嵌套表的对象,这是主要的对象层次的样子(每一划线是一个属性附加伤害子类的):

 文件夹
-name
-List<&子文件夹GT;子文件夹
-name
-List<文件>文件
-name
-key



有对象的这种层次结构,我有文档的名字,我想寻找它返回它的父文件夹(子文件夹)



例如:

 文件夹
-name:客户
-List<&子文件夹GT;子文件夹
-name:个人
-List<文件>文件
-name:恢复
- 键:1

如果我说: 恢复时,LINQ表达式应该返回我:子文件夹个人(对象)



请帮助我,因为两个嵌套列表中我有烦恼,有一个它会很容易。



先谢谢了。


解决方案

 文件夹
.SelectMany(S = GT; s.SubFolders)
.FirstOrDefault(S = GT; s.Documents.Any(D = GT; d.Name ==恢复));



我从这里臀部拍摄,但我认为的的工作....


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.

解决方案
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....

这篇关于LINQ的嵌套列表表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:30