23

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?

0

5 Answers 5

46

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.

3
  • 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
    – Adam
    Jun 27, 2016 at 15:23
  • 3
    The original question mentions that the objects implement ToString(). So, you'll either have to add ToString() functionality to the input classes (if you can), or you can just use linq to project the objects to strings, a la string.Join(", ", myObjects.Select(a => MyStringMethod(a)));.
    – eouw0o83hf
    Jul 11, 2016 at 13:10
  • Also, the documentation does show an overload which takes in Object[] for the second arg. So, assuming myObjects is an array, it will work for straight-up Objects
    – eouw0o83hf
    Jul 11, 2016 at 13:13
27

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) );
3
  • 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 with null values which your solution doesn't.
    – dtb
    Jan 2, 2013 at 17:40
  • and with delimiter like , ?
    – Kiquenet
    Oct 8, 2019 at 13:13
  • 1
    @Kiquenet Use String.Join instead of String.Concat. If you look it up you'll see it has an extra parameter to specify delimiter.
    – AaronLS
    Oct 8, 2019 at 17:16
10

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
  • 2
    This worked from me in Unity where the other answers did not.
    – Chris
    Oct 26, 2016 at 23:19
  • 1
    I'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
    – BrianLegg
    Jan 26, 2017 at 14:36
8

You can use String.Join to concatenate the object list.

string str = String.Join(",", objects);
2
  • .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>.
    – dtb
    Jan 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 with null values which your solution doesn't.
    – dtb
    Jan 2, 2013 at 17:41
0

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; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.