gpt4 book ai didi

c# - 组字符串值

转载 作者:行者123 更新时间:2023-11-30 14:25:20 24 4
gpt4 key购买 nike

我有这个代码

var myBuilder = new StringBuilder();

foreach (var item in myList)
{
myBuilder.Append(item.Number).Append(" - ").Append(item.SecondNumber).Append(", ");
}

var text = myBuilder;

并使用它我得到了这段文字

AAA08 - BB08, AAA09 - BB09, AAA09 - BB10,

myList 返回:

{ Number = "AAA08", SecondNumber = "BB08" }

{ Number = "AAA09", SecondNumber = "BB09" }

{ Number = "AAA09", SecondNumber = "BB10" }

如何将它连接到字符串以显示:

AAA08 - BB08; AAA09 - BB09, BB10

我可以将 , 替换为 ; 但无法了解如何将这些 Number 分组以仅显示一个和每个 SecondNumber 给他的权利

最佳答案

您可以使用 Linq 和 string.Join

var grouped = myList
.GroupBy(x => x.Number)
.Select(g => g.Key + " - " + string.Join(", ", g.Select(x => x.SecondNumber)))
var text = string.Join("; ", grouped);

关于c# - 组字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com