I have the following code:
Dictionary <string, decimal> inventory; // this is passed in as a parameter. It is a map of name to price // I want to get a list of the keys. // I THOUGHT I could just do: List<string> inventoryList = inventory.Keys.ToList(); But I get the following error:
'System.Collections.Generic.Dictionary.KeyCollection' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.Dictionary.KeyCollection' could be found (are you missing a using directive or an assembly reference?)
Am I missing a using directive? Is there something other than
using System.Collections.Generic; that I need?
EDIT
List < string> inventoryList = new List<string>(inventory.Keys); works, but just got a comment regarding LINQ
using System.Linq;, because that's where the.ToList()extension method is definednew List<string>(inventory.Keys)Enumerable.ToList(inventory.Keys), because ToList<T>() isList<TSource> ToList<TSource>(this IEnumerable<TSource> source), where thethis IEnumerable<TSource> sourcecan even be passed explicitly (useful in the debugger, when you don't have the usingSystem.Linqdefinde)Enumerableclass is inSystem.Linqso namespace needs to be added anyways.System.Linq.Enumerable.ToList(inventory.Keys)