你如何嵌套字典并指向另一个字典中的特定键
How do you nest dictionaries and point to specific keys in another dictionary
因此,首先我创建了 3 个相互正确嵌套的词典,然后我毫无问题地将 keys/values 添加到 gdicRawItems 词典中。现在我正在向 gdicPreppedItems 字典添加一个键,但我需要它指向 gdicRawItems 中的多个 key/values,这样做的正确语法是什么?本质上,当我查看 PreppedItems 字典并查看键“Cheesburger”时,我希望看到值是 Beef Patty、Bun 和 Cheese Slice
Public Class frmDishDesigner
Public gdicRawItems As New SortedDictionary(Of String, String)
Public gdicPreppedItems As New SortedDictionary(Of String, SortedDictionary(Of String, String))
Public gdicDishes As New SortedDictionary(Of String, SortedDictionary(Of String, SortedDictionary(Of String, String)))
Public Sub Main()
End Sub
Public Sub frmDishDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gdicRawItems.Add("Beef Patty", "Beef Patty")
gdicRawItems.Add("Cheese Slice", "Cheese Slice")
gdicRawItems.Add("Bun", "Bun")
gdicRawItems.Add("Water", "Water")
gdicRawItems.Add("Potato", "Potato")
gdicRawItems.Add("Mushroom", "Mushroom")
gdicRawItems.Add("Plate", "Plate")
gdicPreppedItems.Add("Cheesburger", (gdicRawItems.Item(Bun)))
gdicPreppedItems.Add("
你不能随心所欲。您必须添加一个字典,但您只有一个包含所有原料的字典。
gdicPreppedItems.Add("Cheesburger", gdicRawItems)
这将添加所有原始项目。
我们正在处理何时需要数据库的示例。您可以使用 类 和列表,但这是在设计时构建的,如果您想更改配方,则需要重新编译代码:
Public Class PreppedItem
Public Property Name As String
Public Property RawIngredients As New List(Of String)
End Class
Public Class Dish
Public Property Name As String
Public Property PreppedItems As New List(Of PreppedItem)
End Class
Dim cheeseburgerDish As New Dish() With {
.Name = "Cheeseburger Dish",
.PreppedItems = New List(Of PreppedItem) From {
New PreppedItem() With {.Name = "Cheeseburger",
.RawIngredients = New List(Of String) From {"Beef Patty", "Cheese Slice", "Bun"}
},
New PreppedItem() With {.Name = "Fries",
.RawIngredients = New List(Of String) From {"Potato"}
}
}
}
使用您的类型,您可以制作一道菜,但当您尝试添加另一道菜时它会损坏。因为这道菜包含准备好的物品字典,但如果你为下一道菜添加到它,那么它也会为第一道菜添加。所以你的设计是不可能的。
gdicRawItems.Add("Beef Patty", "Beef Patty")
gdicRawItems.Add("Cheese Slice", "Cheese Slice")
gdicRawItems.Add("Bun", "Bun")
gdicRawItems.Add("Water", "Water")
gdicRawItems.Add("Potato", "Potato")
gdicRawItems.Add("Mushroom", "Mushroom")
gdicRawItems.Add("Plate", "Plate")
Dim cheeseBurger = New SortedDictionary(Of String, String)
cheeseBurger.Add("Bun", gdicRawItems("Bun"))
cheeseBurger.Add("Beef Patty", gdicRawItems("Beef Patty"))
cheeseBurger.Add("Cheese Slice", gdicRawItems("Cheese Slice"))
Dim fries = New SortedDictionary(Of String, String)
fries.Add("Potato", gdicRawItems("Potato"))
gdicPreppedItems.Add("Cheeseburger", cheeseBurger)
gdicPreppedItems.Add("Fries", fries)
gdicDishes.Add("Cheeseburger Dish", gdicPreppedItems)
如果使用数据库,你可以这样设计。让你知道你想做什么。
成分
ID
Name
1
Beef Patty
2
Cheese Slice
3
Bun
4
Water
5
Potato
6
Mushroom
7
Plate
PreppedItem
ID
Name
1
Cheeseburger
2
Fries
3
Mushroom Burger
4
Hamburger
PreppedItemIngredient
ID
PreppedItemID
IngredientID
Quantity
1
1
1
1
2
1
2
1
3
1
3
2
4
2
5
1
5
3
1
1
6
3
6
1
7
3
3
2
8
4
1
1
9
4
3
2
盘子
ID
Name
1
Cheeseburger Plate
2
Mushroom Burger Plate
PlatePreppedItem
ID
PlateID
PreppedItemID
1
1
1
2
1
2
3
2
3
4
2
2
现在要更改食谱,您只需更改数据库,而不是您的代码。关于数量和计量单位,您可以在此处做更多的事情,但这是基本描述。
因此,首先我创建了 3 个相互正确嵌套的词典,然后我毫无问题地将 keys/values 添加到 gdicRawItems 词典中。现在我正在向 gdicPreppedItems 字典添加一个键,但我需要它指向 gdicRawItems 中的多个 key/values,这样做的正确语法是什么?本质上,当我查看 PreppedItems 字典并查看键“Cheesburger”时,我希望看到值是 Beef Patty、Bun 和 Cheese Slice
Public Class frmDishDesigner
Public gdicRawItems As New SortedDictionary(Of String, String)
Public gdicPreppedItems As New SortedDictionary(Of String, SortedDictionary(Of String, String))
Public gdicDishes As New SortedDictionary(Of String, SortedDictionary(Of String, SortedDictionary(Of String, String)))
Public Sub Main()
End Sub
Public Sub frmDishDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gdicRawItems.Add("Beef Patty", "Beef Patty")
gdicRawItems.Add("Cheese Slice", "Cheese Slice")
gdicRawItems.Add("Bun", "Bun")
gdicRawItems.Add("Water", "Water")
gdicRawItems.Add("Potato", "Potato")
gdicRawItems.Add("Mushroom", "Mushroom")
gdicRawItems.Add("Plate", "Plate")
gdicPreppedItems.Add("Cheesburger", (gdicRawItems.Item(Bun)))
gdicPreppedItems.Add("
你不能随心所欲。您必须添加一个字典,但您只有一个包含所有原料的字典。
gdicPreppedItems.Add("Cheesburger", gdicRawItems)
这将添加所有原始项目。
我们正在处理何时需要数据库的示例。您可以使用 类 和列表,但这是在设计时构建的,如果您想更改配方,则需要重新编译代码:
Public Class PreppedItem
Public Property Name As String
Public Property RawIngredients As New List(Of String)
End Class
Public Class Dish
Public Property Name As String
Public Property PreppedItems As New List(Of PreppedItem)
End Class
Dim cheeseburgerDish As New Dish() With {
.Name = "Cheeseburger Dish",
.PreppedItems = New List(Of PreppedItem) From {
New PreppedItem() With {.Name = "Cheeseburger",
.RawIngredients = New List(Of String) From {"Beef Patty", "Cheese Slice", "Bun"}
},
New PreppedItem() With {.Name = "Fries",
.RawIngredients = New List(Of String) From {"Potato"}
}
}
}
使用您的类型,您可以制作一道菜,但当您尝试添加另一道菜时它会损坏。因为这道菜包含准备好的物品字典,但如果你为下一道菜添加到它,那么它也会为第一道菜添加。所以你的设计是不可能的。
gdicRawItems.Add("Beef Patty", "Beef Patty")
gdicRawItems.Add("Cheese Slice", "Cheese Slice")
gdicRawItems.Add("Bun", "Bun")
gdicRawItems.Add("Water", "Water")
gdicRawItems.Add("Potato", "Potato")
gdicRawItems.Add("Mushroom", "Mushroom")
gdicRawItems.Add("Plate", "Plate")
Dim cheeseBurger = New SortedDictionary(Of String, String)
cheeseBurger.Add("Bun", gdicRawItems("Bun"))
cheeseBurger.Add("Beef Patty", gdicRawItems("Beef Patty"))
cheeseBurger.Add("Cheese Slice", gdicRawItems("Cheese Slice"))
Dim fries = New SortedDictionary(Of String, String)
fries.Add("Potato", gdicRawItems("Potato"))
gdicPreppedItems.Add("Cheeseburger", cheeseBurger)
gdicPreppedItems.Add("Fries", fries)
gdicDishes.Add("Cheeseburger Dish", gdicPreppedItems)
如果使用数据库,你可以这样设计。让你知道你想做什么。
成分
ID | Name |
---|---|
1 | Beef Patty |
2 | Cheese Slice |
3 | Bun |
4 | Water |
5 | Potato |
6 | Mushroom |
7 | Plate |
PreppedItem
ID | Name |
---|---|
1 | Cheeseburger |
2 | Fries |
3 | Mushroom Burger |
4 | Hamburger |
PreppedItemIngredient
ID | PreppedItemID | IngredientID | Quantity |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1 | 2 | 1 |
3 | 1 | 3 | 2 |
4 | 2 | 5 | 1 |
5 | 3 | 1 | 1 |
6 | 3 | 6 | 1 |
7 | 3 | 3 | 2 |
8 | 4 | 1 | 1 |
9 | 4 | 3 | 2 |
盘子
ID | Name |
---|---|
1 | Cheeseburger Plate |
2 | Mushroom Burger Plate |
PlatePreppedItem
ID | PlateID | PreppedItemID |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 2 | 3 |
4 | 2 | 2 |
现在要更改食谱,您只需更改数据库,而不是您的代码。关于数量和计量单位,您可以在此处做更多的事情,但这是基本描述。