I have a list of strings and I want to dump them out as a string with semi-colon delimiters.
IEnumerable<string> foo = from f in fooList
where f.property == "bar"
select f.title;
I now want to output this:
title1;title2;title3;title4
How do I do that?
Use the String.Join Method
Using LINQ instead of String.Join as that was what was asked for. Though really String.Join
is probably a safer / easier bet.
IEnumerable<string> foo = from f in fooList
where f.property == "bar"
select f.title;
string join = foo.Aggregate((s, n) => s + ";" + n);
string result = string.Join(";", fooList.Where(x=>x.property == "bar").Select(x=>x.title));
Since .NET 2.0, the string class provides a convenient Join method. While it originally operated on arrays only, .NET 4 adds an IEnumerable overload...
IEnumerable<string> foo = from f in fooList
where f.property == "bar"
select f.title;
Console.WriteLine(string.Join(";", foo));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With