LINQ:基于分组在子列表属性 [英] LINQ: grouping based on property in sublist

查看:171
本文介绍了LINQ:基于分组在子列表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'尝试使用LINQ来创建文档基于元数据的分组列表,它是在文档列表。

下面是如何我的对象结构如下:

 名单,其中,文件>
          - >名单<元数据>
                       - >元数据有一个名字和一个value属性。
 

我想组在此基础上有一个名字的元数据标签的文件:ID和他们群里的ID属性的值是相同的。

我想它是这样的:

 变种X = response.Document
         .GroupBy(D => d.Metadata.Where(DC => dc.Name == DocProperty.ID)
         。选择(DC => dc.Value));
 

这导致在单个文档的列表,而不是分组的ID。

也想过通过文件列表中选择ID的一个独特的列表,然后循环,找到匹配的ID文件。那一个似乎是一个很大的开销,因为在不同的列表中的每个ID我有充分的时间去到元数据列表,找到的文件,并有额外的多个项目的检查发现,得到我需要等属性。

任何人有关于如何让这件事的工作是个好主意?

解决方案

  VAR X =从文档源
        从元在doc.Metadata
        其中,meta.Name == DocProperty.Id
        DOC组由meta.Value;
 

或(评论)流利的符号:

 变种Y =源
    .SelectMany(DOC => doc.Metadata,(文件,元)=>新建{文档,元})
    。凡(双=> pair.meta.Name == DocProperty.Id)
    .GroupBy(双=> pair.meta.Value,对=> pair.doc);
 

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?

解决方案

var x = from doc in source
        from meta in doc.Metadata
        where meta.Name == DocProperty.Id
        group doc by meta.Value;

Or (comments) as fluent notation:

var y = source
    .SelectMany(doc => doc.Metadata, (doc, meta) => new { doc, meta })
    .Where(pair => pair.meta.Name == DocProperty.Id)
    .GroupBy(pair => pair.meta.Value, pair => pair.doc);

这篇关于LINQ:基于分组在子列表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆