90

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();
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85

7 Answers7

93

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>();
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 25
    Instead of OfType(which filters the list) use Select(i => i.ToString()) – Geoff Appleford May 22 '09 at 10:14
  • 1
    @geoff: Depends on what you're looking for. If an element is null, i.ToString will fail. You might also consider Cast(); – Mehrdad Afshari May 22 '09 at 19:43
  • 2
    @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. – Daniel Schaffer Jul 16 '13 at 15:20
  • 3
    @DanielSchaffer I read the question as: you have a `List` that contains `string` objects only and you want to get a `List` 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`, `Cast()`, or `Select(...)`. – Mehrdad Afshari Jul 16 '13 at 20:25
66

This works for all types.

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

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

var stringList = myList.ConvertAll(obj => obj.SomeToStringMethod());
Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
12

You mean something like this?

List<object> objects = new List<object>();
var strings = (from o in objects
              select o.ToString()).ToList();
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • 1
    Just do this: List strings = (from o in objects select o.ToString()).ToList(); – steve_c Jan 26 '09 at 16:41
  • 1
    I needed to convert a list of (app) User objects to a List 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();` – Developer63 Nov 20 '15 at 02:54
6
List<string> myList Str = myList.Select(x=>x.Value).OfType<string>().ToList();

Use "Select" to select a particular column

gaurav
  • 75
  • 1
  • 3
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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
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.

Ben Robbins
  • 2,600
  • 2
  • 24
  • 26