在 .NET 的输出字符串中将列表的所有项目连接在一起 [英] Join together all items of a list in an output string in .NET
本文介绍了在 .NET 的输出字符串中将列表的所有项目连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写一个 LINQ 表达式(或其他任何东西)来从 List 中选择一个项目并将它们连接在一起?
How can I write a LINQ expression (or anything else) that selects an item from a List and join them together?
IList<string> data = new List<string>();
data.Add("MyData1");
data.Add("MyData2");
string result = // Some LINQ query... I try data.Select(x => x + ",");
//result = "MyData1, MyData2"
推荐答案
Just go with (String.Join 方法):
Just go with (String.Join Method):
string joined = String.Join(",", data.ToArray());
但如果必须是 LINQ,您可以尝试:
But if it has to be LINQ, you could try:
string joinedLinq = data.Aggregate((i, j) => i + "," + j);
这篇关于在 .NET 的输出字符串中将列表的所有项目连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文