【发布时间】:2014-07-11 23:58:57
【问题描述】:
假设
有一个趋势类。
public class Trend{
public string TrendName { get; set; }
public string Description { get; set; }
}
例如
new Trend() { TrendName= "Pizza", Description="yum yum" };
new Trend() { TrendName= "Clothing", Description="ba yum" };
new Trend() { TrendName= "Food" };
有一个 Person 类。
public class Person {
public string PersonName { get; }
public int PersonId { get; }
public int aptitude { get; }
...... many other properties
}
例如
new Person() { PersonName = "Arnold Palmer" };
new Person() { PersonName = "Ken Hemingway" };
有一个东西类。
public class TrendyItem
{
public string ItemName { get; }
public string ItemId { get; }
}
例如
new TrendyItem() { ItemName = "PotatoPizza" }
new TrendyItem() { ItemName = "PeplumSkirt" }
有一个 TrendysOfYear 类。这个类已经有了。
public class TrendProfile
{
public List<Trend> FavoriteTrendsOfYear;
public List<Person> ActivePeopleThisYear;
public List<TrendyItem> TrendyItemsThisYear;
}
对于每一个 TrendysOfYear, 会有一个不同趋势的列表,FavoriteTrendsOfYear,
属于 ActivePeopleThisYear 的每个人
将指定一个“TrendyItem”
给定一个 TrendProfile,我希望能够快速查找
1) 输入:人;输出:个人对流行物品的选择列表。
2) 输入:趋势;输出:属于该趋势的流行物品列表。
我考虑了两种方法。
A)Dictionary<Person, Dictionary<Trend, TrendyItem>>
你可以得到 PersonsChoiceOnTrendyItem = dic[Person].Values.ToList();
但每次查找 TrendyItemsOfTrend 时都必须循环并构建新列表。
B)Dictionary<Trend, Dictionary<Person, TrendyItem>>
副题。
将这些自定义对象用作字典键是否是一种好习惯?
使用嵌套字典是个好习惯吗?
在这种情况下,映射项目的最佳方式是什么?
另外,不是Trend类没有整数Id,所以必须使用字符串(保证趋势的名称是唯一的)作为key。
附加信息:TrendName 和Description 等Trend 属性是可编辑的。所以我有点犹豫是否将 TrendyItems 集合添加到 Trend 类。如果 TrendProfile1 和 TrendProfile2 中存在 Trend、“Fashionn”,并且有人决定将名称更改为“Fashion”,我希望两个配置文件都引用同一个对象。
【问题讨论】:
-
当你想在一年中添加一件时髦单品时,它总是与一个人联系在一起吗?也就是说,当年的比萨名单是由人们挑选的所有当年流行的比萨组成的吗?还是先列出比萨饼,然后人们从该列表中投票?
-
另外,每个人是只选择一种趋势,还是可以选择多种?
-
是:“由人们挑选的当年流行的所有披萨组成的年度披萨列表”,每个人可以根据趋势选择一个或NULL。
标签: c# dictionary