En utilisant LINQ, cela devrait fonctionner;
string delimiter = ",";
List<string> items = new List<string>() { "foo", "boo", "john", "doe" };
Console.WriteLine(items.Aggregate((i, j) => i + delimiter + j));
Description de la classe:
public class Foo
{
public string Boo { get; set; }
}
Utilisation:
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();
}
}
Et voici mon meilleur :)
items.Select(i => i.Boo).Aggregate((i, j) => i + delimiter + j)
Pourquoi linq et pas string.Join()? – Alan
string.Join est meilleur mais je pense que linq rend votre code amusant, cela pourrait être le pourquoi! –
String.Join est meilleur car il utilise un StringBuilder et évite les performances O (n^2) d'une concaténation répétée. –