过滤和组合数组中的数据
Posted
技术标签:
【中文标题】过滤和组合数组中的数据【英文标题】:Filtering and combining data in an array 【发布时间】:2020-11-03 16:42:12 【问题描述】:我有一个数组:
"Items":[
"folder":"Test","number":"11",
"folder":"Test","number":"10",
"folder":"Test1","number":"130",
"folder":"Test1","number":"100",
"folder":"Test2","number":"",
"folder":"Test2","number":"200"
]
告诉我我需要使 文件夹 是唯一的,并将其值写在 number 中,用逗号分隔?
"Items":[
"folder":"Test","number":"11, 10",
"folder":"Test1","number":"130, 100",
"folder":"Test2","number":"200"
]
【问题讨论】:
【参考方案1】:您可以使用GroupBy
按文件夹分组,使用String.Join()
连接数字,例如:
var result = Items
.GroupBy(x => x.folder)
.Select(x => new
folder = x.Key,
number = string.Join(",", x.Select(y => y.number).Where(z => !string.IsNullOrEmpty(z)))
).ToList();
在 dotnetfiddle 中测试:https://dotnetfiddle.net/FDvSmc
【讨论】:
以上是关于过滤和组合数组中的数据的主要内容,如果未能解决你的问题,请参考以下文章