Search code examples
c#.netlinq.net-4.5icollection

Unable to use ICollection.ToList()


I am trying to call ToList() here:

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace CSVStuff
{
    public static class CSVContentGenerator
    {
        public static string GetContent(IOrderedDictionary headingsPropertiesMapping)
        {
            var propertyNames = headingsPropertiesMapping.Keys.ToList(); //ICollection does not contain a definition for ToList()
            //var propertyNames = new List<object>(headingsPropertiesMapping.Keys); //Cannot convert from ICollection to int
            return "";
        }
    }
}

Why are these not working?


Solution

  • Try this:

    var propertyNames = headingsPropertiesMapping.Keys.Cast<T>().ToList();
    

    and type T is the type of dictionary keys.