Reputation: 1182
I need to convert from
object {object[]}
to
System.Collections.Generic.List<int>
Inside each element in the object array there is a object {int}
element.
I got this structure from a COM dll.
I was wondering how is the easiest way to do that. Thanks in advance!
Upvotes: 4
Views: 13213
Reputation: 8311
My attempt on this converts the object
array into a int
array and checks if there are no null
entries in the original object array. This is how I go on converting my object array to a List<int>
:
using System.Linq;
//Your object array depends on your data source. I am taking an example of json data
object[] myObjectArray= jsonData["data"];
int[] myIntegerArray= myObjectArray.Where(x => x != null).Select(x => Convert.ToInt32(x)).ToArray();
List<int> myIntegerList= myIntegerArray.ToList();
Upvotes: 1
Reputation: 5801
List<Int> ObjToList(object[] objects)
{
List<int> intList = new list<int>();
foreach (object o in objects)
{
intList.Add((int)o);
}
return intList;
}
be very sure that all your objects in the array are of type int to avoid problems
Upvotes: 3