LINQ:根据子列表中的属性进行分组
我正在尝试使用 LINQ 创建基于元数据的文档分组列表,元数据是文档上的列表。
下面是我的对象结构的外观:
List<Document>
--> List<Metadata>
--> Metadata has a name and a value property.
我想根据名称为 ID 的元数据标签对文档进行分组,并在 ID 属性的值相同的情况下对它们进行分组。
我尝试了这样的操作:
var x = response.Document
.GroupBy(d => d.Metadata.Where(dc => dc.Name == DocProperty.ID)
.Select(dc => dc.Value));
这会生成单个文档的列表,但不会按 ID 分组。
还考虑过选择一个不同的 ID 列表,然后循环遍历文档列表并查找与 ID 匹配的文档。这似乎是很大的开销,因为对于不同列表中的每个 ID,我每次都必须进入元数据列表并查找文档,并且必须对找到的多个项目进行额外检查,获取我需要的属性等
。关于如何让这个东西发挥作用的好主意?
I'am trying to use LINQ to create a grouped list of documents based on metadata which is a list on the document.
Below is how my object structure looks:
List<Document>
--> List<Metadata>
--> Metadata has a name and a value property.
I want to group the documents based on an metadata tag which has a name: ID and group them where the values for the ID property are the same.
I tried it like this:
var x = response.Document
.GroupBy(d => d.Metadata.Where(dc => dc.Name == DocProperty.ID)
.Select(dc => dc.Value));
This results in a list of single documents, but not grouped on ID.
Also thought about selecting a distinct list of ID's and then loop through the document list and find documents that match the ID. That one seems like a lot of overhead, because for every ID in the distinct list i have to go every time into the metadata list and find the documents and have to extra checks for multiple items found, get the property i need etc.
Anyone has a good idea about how to get this thing working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者(注释)作为流畅的表示法:
Or (comments) as fluent notation: