ToList<T>(Func<object, T> func)

public static List<T> ToList<T>(this Array items, Func<object, T> mapFunction)
        {
            if (items == null || mapFunction == null)
                return new List<T>();            

            List<T> coll = new List<T>();
            for (int i = 0; i < items.Length; i++)
            {
                T val = mapFunction(items.GetValue(i));
                if(val != null)
                    coll.Add(val);
            }
            return coll;
        }
Example:
--> Make another extension method

public static List<T> ToList<T>(this object[] items)
        {
            return items.ToList<T>(o => { return (T)o; });
        }

Reduces this : http://extensionmethod.net/Details.aspx?ID=351

To this:

public static List<T> EnumToList<T>() where T : struct
        {
            return Enum.GetValues(typeof(T)).ToList<T>(enumVal => { return (T)Enum.Parse(typeof(T), enumVal.ToString()); });
        }

--> Use in Linq

var myItems = from i in array.ToList<MyType>( o => { return (MyType)o; }) select i;

Description

Converts an array of any type to List<T> passing a mapping delegate Func<object, T> that returns type T. If T is null, it will not be added to the collection. If the array is null, then a new instance of List<T>() is returned.

Details

Double click on the code to select all.

 

;