У меня есть следующий код:В C# как получить список ключей из словаря?
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();
Но я получаю следующее сообщение об ошибке:
'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?)
я упускаю с помощью директивы? Есть ли что-то иное, чем
using System.Collections.Generic;
что мне нужно?
EDIT
List < string> inventoryList = new List<string>(inventory.Keys);
работает, но только что получил комментарий о LINQ
да, вам также нужно использовать System.Linq; ', потому что там определен метод расширения .ToList()'. – GolfWolf
А, ок, спасибо. – Tim
Не включайте System.Linq; если вы * только * нуждаетесь в ToList() для этого экземпляра once, просто вызовите 'new List (inventory.Keys)' –
Alan