Al usar LINQ, esto debería funcionar; Descripción
string delimiter = ",";
List<string> items = new List<string>() { "foo", "boo", "john", "doe" };
Console.WriteLine(items.Aggregate((i, j) => i + delimiter + j));
clase:
public class Foo
{
public string Boo { get; set; }
}
Uso:
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();
}
}
Y aquí es mi mejor :)
items.Select(i => i.Boo).Aggregate((i, j) => i + delimiter + j)
¿Por qué linq y no string.Join()? – Alan
string.Join es mejor, pero creo que linq hace que tu código sea divertido, ¡esa podría ser la razón! –
String.Join es mejor porque usa un StringBuilder y evita el rendimiento O (n^2) heredado de la concatenación repetida. –