93

Is there a way to convert a List(of Object) to a List(of String) in c# or vb.net without iterating through all the items? (Behind the scenes iteration is fine – I just want concise code)

Update: The best way is probably just to do a new select

myList.Select(function(i) i.ToString()).ToList();

or

myList.Select(i => i.ToString()).ToList();

7 Answers 7

95

Not possible without iterating to build a new list. You can wrap the list in a container that implements IList.

You can use LINQ to get a lazy evaluated version of IEnumerable<string> from an object list like this:

var stringList = myList.OfType<string>();
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of OfType(which filters the list) use Select(i => i.ToString())
@geoff: Depends on what you're looking for. If an element is null, i.ToString will fail. You might also consider Cast<string>();
@MehrdadAfshari: Regardless of null handling, OfType() will filter out items that aren't strings - it won't attempt to cast or call ToString(), they'll just get filtered out. That's not what was requested.
@DanielSchaffer I read the question as: you have a List<object> that contains string objects only and you want to get a List<string> of those objects instead. The question is unclear about what the input is; depending on the input specification and your goal, you may want to pick OfType<string>, Cast<string>(), or Select(...).
69

This works for all types.

List<object> objects = new List<object>();
List<string> strings = objects.Select(s => (string)s).ToList();

Comments

27

If you want more control over how the conversion takes place, you can use ConvertAll:

var stringList = myList.ConvertAll(obj => obj.SomeToStringMethod());

1 Comment

Yes it DOES, but it's behind the scenes.
12

You mean something like this?

List<object> objects = new List<object>();
var strings = (from o in objects
              select o.ToString()).ToList();

2 Comments

Just do this: List<string> strings = (from o in objects select o.ToString()).ToList();
I needed to convert a list of (app) User objects to a List<string> for an environment migration use case. A minor adaptation of this approach worked great for me. var userIDsSource = (from r in MigrationSession.UsersSource select r.UserID).ToList();
7
List<string> myList Str = myList.Select(x=>x.Value).OfType<string>().ToList();

Use "Select" to select a particular column

Comments

3

No - if you want to convert ALL elements of a list, you'll have to touch ALL elements of that list one way or another.

You can specify / write the iteration in different ways (foreach()......, or .ConvertAll() or whatever), but in the end, one way or another, some code is going to iterate over each and every element and convert it.

Marc

Comments

1

Can you do the string conversion while the List(of object) is being built? This would be the only way to avoid enumerating the whole list after the List(of object) was created.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.