6

object list

I have List<object> that seems above, I want to convert it to List<string>.

How can I convert it ?

I need List<string> that has 6 items (11:00,13:45,.... etc)

4
  • 1
    You don't have a List<object> you have a object[] of size 6. Commented Oct 17, 2013 at 20:16
  • @ScottChamberlain, true, but most of the solutions below should still work. Array has IEnumerable. Commented Oct 17, 2013 at 20:17
  • seances is List<object>, and first index of seances is object[]. I need these items. Commented Oct 17, 2013 at 20:17
  • And what do you expect to happen if there is an object that's not a string in there? Should it be excluded, should an exception be thrown, or what? Commented Oct 17, 2013 at 20:32

6 Answers 6

20
var mylist = myObjectList.ConvertAll(x => x.ToString());

Edit

  var mylist = myObjectList.ConvertAll(x => Convert.ToString(x));

thanks Scott Chamberlain

To get first array of objects

var mylist = (myObjectList.First() as object[]).ToList()
                .ConvertAll(x=>Convert.ToString(x));

To add rest to the list.

mylist.AddRange(mylist.GetRange(1,myObjectList.Count-2).ConvertAll(x=>Convert.ToString(x)));
Sign up to request clarification or add additional context in comments.

1 Comment

Huh, I never knew about ConvertAll. However you should use Convert.ToString(x) as that will check if the class supports IConvertable and if not then it falls back to .ToString()
6
var stringList = yourObjectList.OfType<string>().ToList();

Remember to add the namespace System.Linq;

The OfType is needed to convert the array to an array<T> which is necessary in order to use it with LINQ

7 Comments

Unable to cast object of type 'System.Object[]' to type 'System.String'
DO you perhaps have an array as one of your entries?
@MelihMucuk, what do you have for yourObjectList? It should be seanceInfo.theatre[i].seances[0].
there is no options for seanceInfo.theatre[i].seances[0] like Select or any LINQ expression.
Ok, updated. It is just an object array - not an array of T which is needed for Linq to work
|
3

Try this

List<string> stringlist = objectList.Cast<string>()
                                    .ToList();

If you're not certain about those elements are strings you can use Select

List<string> stringlist = objectList.Select(x=> x.ToString())
                                    .ToList();

To avoid NullReferenceException in case of null values try the following

List<string> stringlist = objectList.Where(x=> x != null)
                                    .Select(x=> x.ToString())
                                    .ToList();

2 Comments

The second will fail on null values.
@Servy I anticipated this, got it from you. Fixed now :)
2

Using LINQ this is fairly easy. If you are sure they are all strings you can simply do

int i = //Some code that sets i, like a for loop
var oldList = seanceInfo.theatre[i].seances;
List<string> newList = oldList.Cast<string>().ToList();

If you are not sure all of the objects are strings you need to perform some kind of conversion, however that is just as easy

List<string> newList = oldList.Select(o => Convert.ToString(o)).ToList();

From your comment: "seances is List<object>, and first index of seances is object[]. I need these items.", I think what you really want may be a SelectMany

List<string> info = seanceInfo.theatre.SelectMany(x => x.seances).Select(x => Convert.ToString(x)).ToList();

This will take each seance in each theater and combine it in to one master list.

4 Comments

It works but I need listed items in index of [0]. Your solution doesn't give to me these items.
@MelihMucuk yes it does, See my updated version where I replaced the \\...
Unable to cast object of type 'System.Object[]' to type 'System.String'
@MelihMucuk we all kinda figured that out already and every answer here is trying to give that to you. Update your answer instead with actual code showing what you have tried with our various implmentations. You likely made a error copying from this website to your code and if we could see what you wrote it would help a lot.
1

You can simply cast it using LinQ.

myObjectList.Cast<string>();

Or filter all non-string

myObjectList.OfType<string>();

2 Comments

No such method TypeOf
I would be careful with OfType. It really acts as a where statement looking for objects that are the given type, and weeds out the ones that are not. If for some (dumb) reason you have more than 2 types in your list, or your types are not string to begin with, it will select less.
-4

Casting like :

var list = (List<String>) listObjects.

2 Comments

Ok, I'm calling your bluff. That does not compile.
It's generally a good idea to try out code like this before posting it as an answer.

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.