C# 使用 LINQ 连接 List<string> 中的所有字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/559415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:09:33  来源:igfitidea点击:

Concat all strings inside a List<string> using LINQ

c#linq.net-3.5

提问by Jobi Joy

Is there any easy LINQ expression to concatenate my entire List<string>collection items to a single stringwith a delimiter character?

是否有任何简单的 LINQ 表达式可以将我的整个List<string>集合项连接到一个string带有分隔符的单个项?

What if the collection is of custom objects instead of string? Imagine I need to concatenate on object.Name.

如果集合是自定义对象而不是string? 想象一下,我需要连接object.Name.

采纳答案by Ali Ers?z

By using LINQ, this should work;

通过使用 LINQ,这应该可以工作;

string delimiter = ",";
List<string> items = new List<string>() { "foo", "boo", "john", "doe" };
Console.WriteLine(items.Aggregate((i, j) => i + delimiter + j));

class description:

班级说明:

public class Foo
{
    public string Boo { get; set; }
}

Usage:

用法:

class Program
{
    static void Main(string[] args)
    {
        string delimiter = ",";
        List<Foo> items = new List<Foo>() { new Foo { Boo = "ABC" }, new Foo { Boo = "DEF" },
            new Foo { Boo = "GHI" }, new Foo { Boo = "JKL" } };

        Console.WriteLine(items.Aggregate((i, j) => new Foo{Boo = (i.Boo + delimiter + j.Boo)}).Boo);
        Console.ReadKey();

    }
}

And here is my best :)

这是我最好的:)

items.Select(i => i.Boo).Aggregate((i, j) => i + delimiter + j)

回答by Sedat Kapanoglu

In .NET 4.0 or later versions:

在 .NET 4.0 或更高版本中:

String.Join(delimiter, list);

is sufficient.

足够了。

回答by Jacob Proffitt

Good question. I've been using

好问题。我一直在用

List<string> myStrings = new List<string>{ "ours", "mine", "yours"};
string joinedString = string.Join(", ", myStrings.ToArray());

It's not LINQ, but it works.

它不是 LINQ,但它有效。

回答by Peter McG

List<string> strings = new List<string>() { "ABC", "DEF", "GHI" };
string s = strings.Aggregate((a, b) => a + ',' + b);

回答by Alexander Prokofyev

This is for a string array:

这是一个字符串数组:

string.Join(delimiter, array);

This is for a List<string>:

这是一个 List<string>:

string.Join(delimiter, list.ToArray());

And this is for a list of custom objects:

这是自定义对象列表:

string.Join(delimiter, list.Select(i => i.Boo).ToArray());

回答by dev.bv

using System.Linq;

public class Person
{
  string FirstName { get; set; }
  string LastName { get; set; }
}

List<Person> persons = new List<Person>();

string listOfPersons = string.Join(",", persons.Select(p => p.FirstName));

回答by Jord?o

I think that if you define the logic in an extension method the code will be much more readable:

我认为,如果您在扩展方法中定义逻辑,代码将更具可读性:

public static class EnumerableExtensions { 
  public static string Join<T>(this IEnumerable<T> self, string separator) {  
    return String.Join(separator, self.Select(e => e.ToString()).ToArray()); 
  } 
} 

public class Person {  
  public string FirstName { get; set; }  
  public string LastName { get; set; }  
  public override string ToString() {
    return string.Format("{0} {1}", FirstName, LastName);
  }
}  

// ...

List<Person> people = new List<Person>();
// ...
string fullNames = people.Join(", ");
string lastNames = people.Select(p => p.LastName).Join(", ");

回答by Biddut

I have done this using LINQ:

我已经使用 LINQ 做到了这一点:

var oCSP = (from P in db.Products select new { P.ProductName });

string joinedString = string.Join(",", oCSP.Select(p => p.ProductName));

回答by Nishanth Shaan

You can simply use:

您可以简单地使用:

List<string> items = new List<string>() { "foo", "boo", "john", "doe" };

Console.WriteLine(string.Join(",", items));

Happy coding!

快乐编码!