I want to convert an ArrayList to a List<string> using LINQ. I tried ToList() but that approach is not working:
ArrayList resultsObjects = new ArrayList();
List<string> results = resultsObjects.ToList<string>();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code actually shows a
List<ArrayList>rather than a singleArrayList. If you’re really got just oneArrayList, you’d probably want:The
Castcall is required becauseArrayListis weakly typed – it only implementsIEnumerable, notIEnumerable<T>. Almost all the LINQ operators in LINQ to Objects are based onIEnumerable<T>.That’s assuming the values within the
ArrayListreally are strings. If they’re not, you’ll need to give us more information about how you want each item to be converted to a string.