C# 无法使用带有嵌入列表的词典(如Dictionary<;字符串,列表<;字符串>&燃气轮机;
但是,试图将列表对象添加到字典中,我无法理解这样一个概念,即您拥有它们的密钥,因此您无法这样做 在使用购物清单程序时,我需要将库存添加到客户的购物车中,将其从库存中删除,然后将其添加到字典中C# 无法使用带有嵌入列表的词典(如Dictionary<;字符串,列表<;字符串>&燃气轮机;,c#,list,dictionary,C#,List,Dictionary,但是,试图将列表对象添加到字典中,我无法理解这样一个概念,即您拥有它们的密钥,因此您无法这样做 在使用购物清单程序时,我需要将库存添加到客户的购物车中,将其从库存中删除,然后将其添加到字典中 public static Inventory AddToCart(List<Inventory> Inventory) { // this method moves a dvd from inventory to the shopping cart.
public static Inventory AddToCart(List<Inventory> Inventory)
{
// this method moves a dvd from inventory to the shopping cart.
int i = 0;
int iInput = 0;
Inventory newCart = null;
if (Inventory.Count > 0)
{
foreach (Inventory obj in Inventory)
{
++i;
Console.WriteLine("[" + i + "] " + obj.ToString());
}
iInput = Validation.GetInt("Please Choose an item to add to cart: ");
Console.Write($"Move item at record: #{iInput - 1} to shopping cart: (y or n)");
string sInput = Console.ReadLine();
if (string.IsNullOrEmpty(sInput))
{
Console.WriteLine("\r\nNo Valid number was chosen: ");
}
else
switch (sInput.ToLower())
{
case "y":
{
newCart = Inventory[iInput - 1];
Inventory.RemoveAt(iInput - 1);
}
break;
case "n":
case null:
case "":
{
Console.WriteLine("\r\nNo Valid number was chosen: ");
}
break;
default:
{
Console.WriteLine("Keeping item in Inventory");
}
break;
}
}
else
{
Console.WriteLine("\nNo records were found in the Shopping Cart");
}
return newCart;
}
公共静态库存AddToCart(列表库存)
{
//此方法将dvd从库存移动到购物车。
int i=0;
int iInput=0;
库存newCart=null;
如果(Inventory.Count>0)
{
foreach(库存中的库存对象)
{
++一,;
Console.WriteLine(“[”+i+“]”+obj.ToString());
}
iInput=Validation.GetInt(“请选择要添加到购物车中的项目:”);
Console.Write($“将记录中的项目:{iInput-1}移动到购物车:(y或n)”;
字符串sInput=Console.ReadLine();
if(string.IsNullOrEmpty(sInput))
{
Console.WriteLine(“\r\n未选择有效数字:”);
}
其他的
开关(sInput.ToLower())
{
案例“y”:
{
newCart=存货[iInput-1];
存货转移(iInput-1);
}
打破
案例“n”:
大小写为空:
案例“”:
{
Console.WriteLine(“\r\n未选择有效数字:”);
}
打破
违约:
{
Console.WriteLine(“将项目保存在库存中”);
}
打破
}
}
其他的
{
Console.WriteLine(“购物车中未找到任何记录”);
}
返回newCart;
}
这只使用了一个列表,但是我需要能够将其转换为嵌入此列表的词典string newKey=“newKey”;
string newKey = "NEW KEY";
string firstListElement = "FIRST";
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
if (dict.ContainsKey(newKey)) {
// you need to throw an exception or return some signal to exit function
}
dict.Add(newKey, new List<string>());
dict[newKey].Add(firstListElement);
字符串firstListElement=“FIRST”;
Dictionary dict=新字典();
if(dict.ContainsKey(newKey)){
//您需要抛出异常或返回一些信号以退出函数
}
dict.Add(newKey,newlist());
dict[newKey].Add(firstListElement);
我不确定是否理解了您的问题,但这里有一些关于如何使用包含列表的字典的说明
您有一个由键和列表组成的字典
Dictionary<string, List<int>> dictionaryWithList = new Dictionary<string, List<int>>();
因此,如果您想向包含键“test”的列表中添加一个新编号,则可以使用:
dictionaryWithList["test"].Add(42)
如果要避免因密钥不存在而导致异常,请先测试密钥是否存在:
if(dictionaryWithList.ContainsKey("test"))
如果这对您的问题的进一步信息没有帮助,请通知我:)请按照中的建议做些什么。如果你不想创建一个新的类,你可以只使用helper方法中的逻辑。你已经列出了你想要的东西,但是你没有问任何问题。你的问题是什么?我真的很想帮你,但这里没有真正的问题。这里也没有字典。我也无法理解,因为你命名了一个参数
Inventory
,表示Inventory
对象的列表。这很令人困惑。参数的更好名称是inventoryList
。给你的变量起一个有意义的名字,很多混乱就会消失。
dictionaryWithList["test"].Add(42)
if(dictionaryWithList.ContainsKey("test"))