认为
有一个趋势类。
public class Trend{
public string TrendName { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
例如
new Trend() { TrendName= "Pizza", Description="yum yum" };
new Trend() { TrendName= "Clothing", Description="ba yum" };
new Trend() { TrendName= "Food" };
Run Code Online (Sandbox Code Playgroud)
有一个 Person 类。
public class Person {
public string PersonName { get; }
public int PersonId { get; }
public int aptitude { get; }
...... many other properties
}
Run Code Online (Sandbox Code Playgroud)
例如
new Person() { PersonName = "Arnold Palmer" };
new Person() { PersonName = "Ken Hemingway" };
Run Code Online (Sandbox Code Playgroud)
有一个事物类。
public class TrendyItem
{
public string ItemName { get; }
public string ItemId { get; }
}
Run Code Online (Sandbox Code Playgroud)
例如
new TrendyItem() { ItemName = "PotatoPizza" }
new TrendyItem() { ItemName = "PeplumSkirt" }
Run Code Online (Sandbox Code Playgroud)
有一个 TrendysOfYear 课程。这堂课已经有了。
public class TrendProfile
{
public List<Trend> FavoriteTrendsOfYear;
public List<Person> ActivePeopleThisYear;
public List<TrendyItem> TrendyItemsThisYear;
}
Run Code Online (Sandbox Code Playgroud)
对于每个TrendysOfYear,都会有一个不同趋势的列表,FavoriteTrendsOfYear,
ActivePeopleThisYear 的每个人
将指定一个“TrendyItem”
给定一个 TrendProfile,我希望能够快速查找
1)输入:人;输出:个人对流行单品的选择列表。
2)输入:趋势;输出:属于该趋势的流行单品列表。
我考虑过两种方法。
A)Dictionary<Person, Dictionary<Trend, TrendyItem>>
你可以得到 PersonsChoiceOnTrendyItem = dic[Person].Values.ToList();
但每次查找 TrendyItemsOfTrend 时都必须循环并构建新列表。
二)Dictionary<Trend, Dictionary<Person, TrendyItem>>
副塞拉。
使用这些自定义对象作为字典键是一个好习惯吗?
使用嵌套字典是一个好习惯吗?
在这种情况下映射项目的最佳方法是什么?
另外,并不是说 Trend 类没有整数 Id,因此必须使用字符串(保证趋势的名称是唯一的)作为键。
附加信息:趋势的属性(例如趋势名称和描述)是可编辑的。所以我有点犹豫是否将 TrendyItems 集合添加到 Trend 类中。如果 TrendProfile1 和 TrendProfile2 中有 Trend、“Fashionn”,并且有人决定将名称更改为“Fashion”,我希望两个配置文件引用同一对象。
通常,您不会看到包含以这种方式用作键的值的对象。通常,您会在对象上标识一些唯一的键,并将其用作键,将对象本身用作值。例如,您可以将Person
对象存储在 a 中Dictionary
,其中人名是键,Person
是值。
您应该考虑向对象添加集合,而不是嵌套字典。例如,您可以添加List<TrendyItem>
toTrend
来维持这种关系。
这是组织课程的另一种方法。我不太确定每个集合的范围是什么,但这应该为您提供另一种看待问题的方式。
public class Trend
{
public string TrendName { get; set; }
public string Description { get; set; }
// This maintains the relationship between Trend and TrendyItem
public List<TrendyItem> Items { get; set; }
}
public class Person
{
public string PersonName { get; set; }
public int PersonId { get; set; }
public int aptitude { get; set; }
// Each person will specifiy a "TrendyItem"
public TrendyItem Choice { get; set; }
}
public class TrendyItem
{
public string ItemName { get; set; }
public string ItemId { get; set; }
}
public class TrendProfile
{
// Change this to to a key value pair. The key will be how you uniquely identify (input) the Trend in
//2) Input: Trend; Output: List of trendy items belonging to that trend.
// For example TrendName
public Dictionary<string, Trend> FavoriteTrendsOfYear;
// Change this to to a key value pair. The key will be how you uniquely identify (input) the Person in
// 1) Input: Person; Output: List of Person's choice on trendy items.
// For example PersonName
public Dictionary<string, Person> ActivePeopleThisYear;
public List<TrendyItem> TrendyItemsThisYear;
}
Run Code Online (Sandbox Code Playgroud)
通过该类结构,您可以轻松回答帖子中的问题。
static void Main()
{
TrendProfile trendProfile = new TrendProfile();
trendProfile.FavoriteTrendsOfYear = new Dictionary<string, Trend> {
{ "Pizza", new Trend() {
TrendName = "Pizza",
Description = "yum yum",
Items = new List<TrendyItem> {
new TrendyItem() {ItemName = "PotatoPizza1"},
new TrendyItem() {ItemName = "PotatoPizza2"},
new TrendyItem() {ItemName = "PotatoPizza3"}
}
}},
{ "Clothing", new Trend() {
TrendName = "Clothing",
Description = "ba yum",
Items = new List<TrendyItem> {
new TrendyItem() {ItemName = "PeplumSkirt1"},
new TrendyItem() {ItemName = "PeplumSkirt2"},
new TrendyItem() {ItemName = "PeplumSkirt3"}
}
}}
};
trendProfile.ActivePeopleThisYear = new Dictionary<string, Person> {
{ "Arnold Palmer", new Person() { PersonName = "Arnold Palmer", Choice = trendProfile.FavoriteTrendsOfYear["Pizza"].Items[1] }},
{ "Ken Hemingway", new Person() { PersonName = "Ken Hemingway", Choice = trendProfile.FavoriteTrendsOfYear["Clothing"].Items[2] }},
};
//1) Input: Person; Output: List of Person's choice on trendy items.
string person = "Arnold Palmer";
Console.WriteLine(trendProfile.ActivePeopleThisYear[person].Choice.ItemName);
//2) Input: Trend; Output: List of trendy items belonging to that trend.
string trend = "Clothing";
foreach(TrendyItem item in trendProfile.FavoriteTrendsOfYear[trend].Items)
Console.WriteLine(item.ItemName);
}
Run Code Online (Sandbox Code Playgroud)
更新
要跨趋势配置文件共享趋势,您可以首先创建趋势的“主”List
或Dictionary
趋势。然后,在构建每个趋势配置文件时,您可以从“主”中选择趋势。
// "Master" list of trends
List<Trend> trends = new List<Trend> {
new Trend() {
TrendName = "Pizza",
Description = "yum yum",
Items = new List<TrendyItem> {
new TrendyItem() {ItemName = "PotatoPizza1"},
new TrendyItem() {ItemName = "PotatoPizza2"},
new TrendyItem() {ItemName = "PotatoPizza3"}
}
},
new Trend() {
TrendName = "Clothing",
Description = "ba yum",
Items = new List<TrendyItem> {
new TrendyItem() {ItemName = "PeplumSkirt1"},
new TrendyItem() {ItemName = "PeplumSkirt2"},
new TrendyItem() {ItemName = "PeplumSkirt3"}
}
}
};
TrendProfile trendProfile1 = new TrendProfile();
trendProfile1.FavoriteTrendsOfYear = new Dictionary<string, Trend> {
{ trends[0].TrendName, trends[0] },
{ trends[1].TrendName, trends[1] }
};
TrendProfile trendProfile2 = new TrendProfile();
trendProfile2.FavoriteTrendsOfYear = new Dictionary<string, Trend> {
{ trends[1].TrendName, trends[1] }
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3413 次 |
最近记录: |