我需要转换从
object {object[]}
为了
System.Collections.Generic.List<int>
在对象数组的每个元素中,都有一个
object {int}
元素。
我从一个COM dll中得到了这个结构。我想知道最简单的方法是什么。 提前感谢!
我需要转换从
object {object[]}
为了
System.Collections.Generic.List<int>
object {int}
元素。
我从一个COM dll中得到了这个结构。尝试
List<int> intList = objectArray.Cast<int>().ToList();
Cast<int>
更合适,因为“在每个元素内部…”。额外的类型检查应该是多余的。 - Tim Schmelterobject[] idArray = (object[])idObject;
- ThiagoList<int> idList = idArray.Cast<int>().ToList();
- ThiagoList<Int> ObjToList(object[] objects)
{
List<int> intList = new list<int>();
foreach (object o in objects)
{
intList.Add((int)o);
}
return intList;
}
请确保数组中所有的对象都是int类型,以避免出现问题。
我的尝试是将对象
数组转换为整数
数组,并检查原始对象数组中是否有null
项。这是我将对象数组转换为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();