c#嵌套字典,更好的替代方案?最好地组织三个班级的数据

时间:2014-07-11 23:58:57

标签: c# dictionary

假设

有一个趋势课。

public class Trend{ 
    public string TrendName { get; set; }
    public string Description { get; set; }
}

e.g。

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
}

e.g。

new Person() { PersonName = "Arnold Palmer" };
new Person() { PersonName = "Ken Hemingway" };

有一件事。

public class TrendyItem
{
    public string ItemName { get; }
    public string ItemId { get; }
}

e.g。

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的每个人

将指定&#34; TrendyItem&#34;

鉴于TrendProfile,我希望能够快速查找

1)输入:人;输出:人物在时尚物品上的选择列表。

2)输入:趋势;输出:属于该趋势的时尚物品清单。

我考虑过两种方式。

A)Dictionary<Person, Dictionary<Trend, TrendyItem>>

你可以获得PersonsChoiceOnTrendyItem = dic [Person] .Values.ToList();

但每次查看TrendyItemsOfTrend时都必须遍历并构建新列表。

B)Dictionary<Trend, Dictionary<Person, TrendyItem>>

副cesra。

将这些自定义对象用于字典键是一种好习惯吗?

使用嵌套词典是一种好习惯吗?

在这种情况下,映射项目的最佳方法是什么?

此外,并非Trend类没有整数Id,因此必须使用字符串(趋势的名称保证唯一)作为键。


其他信息:Trend的属性(如TrendName和Description)是可编辑的。所以我有点犹豫要不要将TrendyItems的集合添加到Trend类。如果有趋势,&#34; Fashionn&#34;在TrendProfile1和TrendProfile2中,有人决定将名称更改为&#34; Fashion&#34;,我希望两个配置文件都引用同一个对象。

1 个答案:

答案 0 :(得分:2)

通常,您不会以这种方式看到包含用作键的值的对象。通常,您将在对象上标识一些唯一键,并将其用作键,将对象本身用作值。例如,您可以将Person个对象存储在Dictionary中,其中人名是关键,Person是值。

您应该考虑向对象添加集合,而不是嵌套字典。例如,您可以将List<TrendyItem>添加到Trend以维持该关系。

以下是您可以组织课程的另一种方法。我不完全确定每个集合的范围是什么,但这应该为您提供另一种方法来查看问题。

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; 
}

通过该课程结构,您可以轻松回答帖子中的问题。

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);

}

<强>更新

要在TrendProfiles之间分享趋势,您可以先创建一个&#34; master&#34;趋势ListDictionary。然后在构建每个TrendProfliles时,您可以从&#34; master&#34;中选择趋势。

// "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] }
};
相关问题