gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:23:13 25 4
gpt4 key购买 nike

假设

有一个趋势类。

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

副cesra。

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

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

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

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


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

最佳答案

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

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

这是组织类(class)的另一种方式。我不确定您的每个集合的范围是什么,但这应该为您提供另一种看待问题的方式。

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 共享趋势,您可以首先创建一个“主”ListDictionary的趋势。然后在构建每个 TrendProfliles 时,您可以从“master”中挑选趋势。

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

关于c# 嵌套字典,更好的选择?最好地组织三个类别的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com