[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ArrayList and Linq

Posted on 2011-10-11
Medium Priority
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
  • 2
LVL 16

Assisted Solution

ToddBeaulieu earned 200 total points
ID: 36951973

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 => i.property)

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 75

Accepted Solution

käµfm³d   👽 earned 800 total points
ID: 36952000
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 75

Expert Comment

by:käµfm³d 👽
ID: 36952011

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

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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 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