c#嵌套字典,更好的选择?在三个类中最好地组织数据

c#嵌套字典,更好的选择?在三个类中最好地组织数据,c#,dictionary,C#,Dictionary,假设 有一个趋势类 public class Trend{ public string TrendName { get; set; } public string Description { get; set; } } e、 g 有个人课 public class Person { public string PersonName { get; } public int PersonId { get; } public int aptitude {

假设

有一个趋势类

public class Trend{ 
    public string TrendName { get; set; }
    public string Description { get; set; }
}
e、 g

有个人课

public class Person { 
    public string PersonName { get; }
    public int PersonId { get; }
    public int aptitude { get; }
    ...... many other properties
}
e、 g

有一件事是阶级的

public class TrendyItem
{
    public string ItemName { get; }
    public string ItemId { get; }
}
e、 g

有一个时髦的年夜班。这个班已经有了

public class TrendProfile
{
    public List<Trend> FavoriteTrendsOfYear;
    public List<Person> ActivePeopleThisYear;
    public List<TrendyItem> TrendyItemsThisYear;    
}
公共类趋势配置文件
{
今年最受欢迎的公开名单;
今年公开活动人员名单;
本年度公开上市趋势项目;
}
每一年的潮流, 今年将有一个不同趋势的列表,最受欢迎的趋势

今年属于活跃人群的每个人

将指定“趋势项目”

给定趋势简介,我希望能够快速查找

1) 输入:人;输出:人们对流行项目的选择列表

2) 输入:趋势;输出:属于该趋势的趋势项列表

我考虑过两种方法

A)
字典

您可以获取PersonChoiceOnTrendyItem=dic[Person].Values.ToList()

但每次你查找TrendyItemsOfTrend时,都必须循环并建立新的列表

B)
字典

副塞斯拉

使用这些自定义对象作为字典键是一种好的做法吗

使用嵌套字典是一个好做法吗

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

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



附加信息:趋势的属性(如趋势名称和描述)是可编辑的。所以我有点犹豫是否要将TrendyItems集合添加到Trend类中。如果TrendProfile1和TrendProfile2中存在趋势“Fashionn”,并且有人决定将名称更改为“Fashion”,我希望两个配置文件引用同一个对象

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

代替嵌套字典,应该考虑将集合添加到对象中。例如,您可以将

列表
添加到
趋势
以维护该关系

这里有另一种组织课程的方法。我不确定你们每个收藏的范围是什么,但这应该给你们另一种看待问题的方式

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

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

代替嵌套字典,应该考虑将集合添加到对象中。例如,您可以将

列表
添加到
趋势
以维护该关系

这里有另一种组织课程的方法。我不确定你们每个收藏的范围是什么,但这应该给你们另一种看待问题的方式

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

当你考虑在一年中增加一个流行项目时,它总是与一个人联系在一起吗?也就是说,这一年的比萨饼清单是由所有被选为当年流行的比萨饼组成的吗?或者比萨饼的名单排在第一位,然后人们从名单中投票?此外,每个人是在每个趋势中只选择一件事,还是可以选择多件事?是的:“今年的比萨饼名单由当年被选为流行的所有比萨饼组成”每个人都可以选择一个趋势或空。当你考虑在一年中添加一个趋势项目时,它总是与一个人关联吗?也就是说,这一年的比萨饼清单是由所有被选为当年流行的比萨饼组成的吗?或者比萨饼的名单排在第一位,然后人们从名单中投票?此外,每个人是在每个趋势中只选择一件事,还是可以选择多件事?是的:“今年的比萨饼名单由当年被选为流行的所有比萨饼组成”每个人都可以选择一个趋势或空。谢谢你的建议。我忘了提到“Trend”类可以在不同的“TrendProfile”类中重用。如果趋势相同,我希望两个配置文件引用相同的对象。做这件事的好方法是什么?谢谢你的建议。我忘了提到“Trend”类可以在不同的“TrendProfile”类中重用。如果趋势相同,我希望两个配置文件引用相同的对象。做这件事的好方法是什么?
public class TrendProfile
{
    public List<Trend> FavoriteTrendsOfYear;
    public List<Person> ActivePeopleThisYear;
    public List<TrendyItem> TrendyItemsThisYear;    
}
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);

}
// "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] }
};