I have a list of objects that implement ToString()
. I need to convert the whole list to one string in one line. How can I do that?
5 Answers
Another method that may help out is string.Join()
, which takes a set of objects and will join them with any delimiter you want. For instance:
var combined = string.Join(", ", myObjects);
will make a string that is comma/space separated.
-
Tried this, refused by compiler due to objects not being strings. Docs seem to say this will never work, requires strings, not objects? msdn.microsoft.com/en-us/library/57a79xd0%28v=vs.110%29.aspx– AdamJun 27, 2016 at 15:23
-
3The original question mentions that the objects implement
ToString()
. So, you'll either have to addToString()
functionality to the input classes (if you can), or you can just use linq to project the objects tostring
s, a lastring.Join(", ", myObjects.Select(a => MyStringMethod(a)));
. Jul 11, 2016 at 13:10 -
Also, the documentation does show an overload which takes in
Object[]
for the second arg. So, assumingmyObjects
is an array, it will work for straight-upObject
s Jul 11, 2016 at 13:13
Assuming you mean your objects implement ToString, I believe this will do it:
String.Concat( objects.Select(o=>o.ToString()) );
As per dtb note, this should work as well:
String.Concat( objects );
See http://msdn.microsoft.com/en-us/library/dd991828.aspx
Of course, if you don't implement ToString, you can also do things like:
String.Concat( objects.Select(o=>o.FirstName + " " + o.LastName) );
-
6
.Select(o=>o.ToString())
is unnecessary, as the String.Concat method has an overload that accepts an IEnumerable<T> and applies.ToString
to each item. The method also properly deals withnull
values which your solution doesn't.– dtbJan 2, 2013 at 17:40 -
-
1@Kiquenet Use
String.Join
instead ofString.Concat
. If you look it up you'll see it has an extra parameter to specify delimiter.– AaronLSOct 8, 2019 at 17:16
None of these worked for me. I'm confused, because the docs explicitly say they won't work (require string, not object). But modifying @Adil's original answer (found by looking at the previous revisions), I got a version that works fine:
string.Join( ",", objectList.Select(c=>c.ToString()).ToArray<string>())
EDIT: as per @Chris's comment - I'm using Unity's version of .NET. I used the Microsoft docs as reference, so I'm still confused why this got downvoted, but ... maybe it's a Unity-specific problem that needs this solution.
-
2
-
1I'm using EF so I don't have access to a ToString method I can implement. Your solution worked perfectly with a slight tweek. Here's my method: return string.Join(", ", skills.Select(s => s.Name).ToArray()); - Thanks for the idea Jan 26, 2017 at 14:36
You can use String.Join to concatenate the object list.
string str = String.Join(",", objects);
-
.Cast<string>()
does work if the object isn't already a string. And.ToArray()
is unneccessary, as the String.Join method accepts an IEnumerable<T>.– dtbJan 2, 2013 at 17:36 -
.Select(c=>c.ToString())
is unnecessary, as the String.Join method has an overload that accepts an IEnumerable<T> and applies.ToString
to each item. The method also properly deals withnull
values which your solution doesn't.– dtbJan 2, 2013 at 17:41
You can use Linq Enumerable.Select to select a string object and Enumerable.Aggregate into a string.
string StringConcat = ObjectList.Select(x => { return x.StringValue; }).ToList().Aggregate((a,b) => $"{a},{b}");
Example structure:
ObjectList = List<ObjectClass>();
public class ObjectClass {
public string StringValue { get; set; }
}