c# 2D 数组 - 合并值
Posted
技术标签:
【中文标题】c# 2D 数组 - 合并值【英文标题】:c# 2D array - merging Values [closed] 【发布时间】:2021-05-02 02:41:29 【问题描述】:我有一个这样的二维字符串数组(5*2):
string[,] data= new string[5, 2] "F1","LINK1",
"F1","LINK2",
"F2","LINK3",
"F3","LINK4",
"F3","LINK5";
我想将值分组并合并到新数组中。
输出:
"F1","LINK1,LINK2",
"F2","LINK3",
"F3","LINK4,LINK5"
输出数组 3*2。
【问题讨论】:
您能用 C# 符号表示您的数组吗? “2D”对不同的人意味着不同的东西 同意这些不是二维数组,它们只是文本块。请阅读How to Ask,让您的体验更顺畅 您有string[,]
或string[][]
数组吗?甚至List<List<string>>
?
@Rafalon 我只有字符串[,]
【参考方案1】:
类似的东西:
string[,] data= new string[5, 2] "F1","LINK1",
"F1","LINK2",
"F2","LINK3",
"F3","LINK4",
"F3","LINK5";
var items = Enumerable.Range(0, data.GetLength(0))
.Select(n => new Key = data[n, 0], Link = data[n, 1]);
var query =
from i in items
group i by i.Key
into g
select new []
g.Key,
string.Join(",", g.Select(x => x.Link))
;
var result = query.ToArray();
【讨论】:
谢谢这个作品我也尝试了另一种方式。以上是关于c# 2D 数组 - 合并值的主要内容,如果未能解决你的问题,请参考以下文章