简体   繁体   English

从列表项的不同属性中恢复列表项

[英]Recover a list item, from a list item distinct property

Given the following simple property 鉴于以下简单属性

Class Data
{
        public string AA { get; set; }
        public int    BB { get; set; }
        public int    CC { get; set; }
        ...
    }

and a List of data items 和数据项列表

List<Data> Items = new List<Data>;

which has some example data 其中有一些示例数据

"2002", 3 ,5 ..
"2002", 3 ,5 ..
"2006", 4 ,2 ..
"2002", 3 ,5 ..
"2018", 9 ,7 ..
"2018", 5 ,5 ..

I need to find the distint values of string AA, so I can use the following code 我需要找到字符串AA的distint值,因此可以使用以下代码

   foreach(var itm = Items.Select(x => x.AA).Distinct()))
   {
       use itm ...
   }

which corectly returns itm = string "2002", "2006" and "2018", However I also need to recover some remaining properties ie BB etc for the "Distinctly Selected" item, which will also be distinctly relevant to the primary field. 它从根本上返回itm =字符串“ 2002”,“ 2006”和“ 2018”,但是我还需要为“ Distinctly Selected”项目恢复一些剩余的属性,例如BB等,这也与主字段明显相关。

I have tried manys ways to try to achieve this but have been unsuccessful and so would be grateful if someone could indicate a way how to achieve this. 我尝试了许多方法来实现这一目标,但没有成功,因此如果有人可以指出实现该目标的方法,我将不胜感激。

I have tried to recovey an index to the item, return the base item ie Data which work as far in returning all items in the list. 我试图重新索引该项目的索引,返回基础项目,即在返回列表中的所有项目方面有效的数据。

In practise there are many tens of thousands of data items and I need to recover fields linked to the distint data item. 实际上,有成千上万的数据项,我需要恢复链接到distint数据项的字段。

I currently have a working solution where I extract the distinct items then use the generated list to search for the first matching item, then recover the data. 我目前有一个可行的解决方案,其中提取不同的项目,然后使用生成的列表搜索第一个匹配的项目,然后恢复数据。 It works but its not very elagant as I have to process extra times against the list. 它可以工作,但不是很费劲,因为我必须对列表进行额外的处理。

Note there is no SQL options in this solution, supplied data is XML or JSON solution has to be standalone exe 请注意,此解决方案中没有SQL选项,提供的数据为XML或JSON解决方案必须是独立的exe。

comments welcome 欢迎评论

Thanks 谢谢

Distinct is just a group by without any aggregation. 不同只是一个没有任何聚合的分组。 What you want is to group on AA and then aggregate the values, specifically you want to take the first match, which can be done like this. 您想要的是对AA进行分组,然后汇总值,特别是您要进行第一个匹配,可以这样进行。

Items.GroupBy(x => x.AA, x => x, (x,grp) => grp.First())

Create another variable and apply group by on that variable it should be work. 创建另一个变量并在该变量上应用group by应该可以。

var another_item = Items.GroupBy(x => x.AA).ToList();
foreach(var itm in another_item)
{
  use itm ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM