ArrayList and Linq

Posted on 2011-10-11
Last Modified: 2012-05-12
Hello all,

If I have a arraylist that is filled with Structs.  I want to sort that arraylist based on one of the properties of that struct.  Is there a way to handle that with LINQ?
Question by:sbornstein2
    LVL 16

    Assisted Solution


    Here I'm sorting rows by a specified element in a list that each row has.

    rowData = rowData.OrderBy(r => r.ColumnData[sortColIndex].Value).ToList();

    Your case is even easier.

    mylist.OrderBy(i =>

    For each item, you are specifying the value to be used to sort the list by. You can return anything you need, including sub properties.
    LVL 74

    Accepted Solution

    You can use the AsQueryable or OfType extension methods to make the ArrayList Linq-enabled. For example:
    using System;
    using System.Collections;
    using System.Linq;
    namespace _27391431
        class Program
            static void Main(string[] args)
                ArrayList arr = new ArrayList();
                arr.Add(new MyStruct() { SomeProp = "a" });
                arr.Add(new MyStruct() { SomeProp = "c" });
                arr.Add(new MyStruct() { SomeProp = "y" });
                arr.Add(new MyStruct() { SomeProp = "e" });
                var sortQuery = from strObj in arr.OfType<MyStruct>()
                                orderby strObj.SomeProp ascending
                                select strObj;
                foreach (var item in sortQuery)
        public struct MyStruct
            public string SomeProp { get; set; }

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽

    One benefit of using the OfType approach like I did above is that since ArrayList is a non-generic collection and can store objects of any type, the OfType call will only return objects that are of the type specified within the angle brackets (MyStruct above) and will effectively skip over any elements which do not match that type within the ArrayList.

    Author Closing Comment

    Thanks guys I actually used the OfType yesterday before these posts but both were helpful.  Thanks again all.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now