4

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());
1
  • The Cast<T> extension casts each element of an IEnumerable and returns them as another IEnumerable. It is therefore not useful to convert the collection itself. Commented Feb 14, 2012 at 12:23

4 Answers 4

15

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't ArrayList need an ICollection? msdn.microsoft.com/en-us/library/…
Yeah, but this is parameter-less. I wanted to point out that ArrayList(IEnumerable) does not exist.
Yes, that's why b is converted to an array with ToArray(). System.Array implements ICollection. You could also use a List of course.
Oh, sorry! I missed this, this is my fault.
Be aware that this will double the memory-allocations. First everything will be collected into the array, and afterwards the entire array is copied again into the ArrayList. However I can´t see any way around this.
4

I'd suggest you to use a List<T> rather than an ArrayList. You can actually use the ToList extension method or the List's constructor which takes an IEnumerable<T>:

var myList = b.ToList(); // either
var myListTwo = new List<myClass>(b); // or

List<T> was newly introduced with .NET 2.0 and is generic. This means it yields you values of your actual type at compile-time, which is myClass, instead of object.

Edit: If you actually need an ArrayList, you need to copy b twice, as it cannot deal with IEnumerable directly, as devdigital pointed out in his reply:

ArrayList arrayList = new ArrayList(b.ToArray());

1 Comment

I'm assuming he needs an ArrayList for some legacy API.
2

You can convert your IEnumerable to an array with ToArray(), then construct an ArrayList from that array.

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

Comments

0
ArrayList arrayList = new ArrayList(b.ToList());

Comments

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.