且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通用转换为IEnumerable的数组列表

更新时间:2022-11-27 22:39:44

一个构造函数为的ArrayList 类型需要一个的ICollection ,所以你应该能够做到以下几点:

 变种b = 
(从MyObj中MyCollection中
选择新MyClass的
{
名称= myObj.Name,
AC = myObj.ac
})ToArray的();

ArrayList的myArrayList =新的ArrayList(B);


I need to convert the linq query from generic ienumerable to arraylist.

ArrayList myArrayList = new ArrayList();
         var b =
              (from myObj in myCollection
           select new myClass
           {
            Name = myObj.Name,
            ac = myObj.ac
           });

I have tried doing

b.Cast<ArrayList>();

but it is not working.

Edited : I got it working with @devdigital solution

but i will also want to point out that at same time i found a hackish solution.

myArrayList.InsertRange(0, b.ToArray());

One of the constructors for the ArrayList type takes an ICollection, so you should be able to do the following:

var b =
    (from myObj in myCollection
     select new myClass
    {
      Name = myObj.Name,
      ac = myObj.ac
    }).ToArray();

ArrayList myArrayList = new ArrayList(b);