【发布时间】:2020-07-01 01:51:50
【问题描述】:
所以我需要用one Key、sub Key和Value创建一个数据结构。以及添加键、子键和值的方法。
键是唯一的,可以有多个 子键。
子键在键内的上下文中是唯一的,可以有多个值。
值在子键的上下文中是唯一的。
我想到的第一个数据结构是字典(执行时间对于任务很重要)。 然后我创建了以下字典。
public class NewCollection : IHoplonCollection
{
class SubIndexAndValue
{
public int subIndex;
public List<string> Value = new List<string>();
}
class DataStructure : IComparable<DataStructure>
{
public string Key;
public List<SubIndexAndValue> subIndexValue = new List<SubIndexAndValue>();
public int CompareTo(DataStructure other)
{
if (Key.CompareTo(other.Key) > 0)
{
return 1;
}else if (Key.CompareTo(other.Key) < 0)
{
return -1;
}else
{
return 0;
}
}
}
SortedDictionary<DataStructure, SubIndexAndValue> colList = new SortedDictionary<DataStructure, SubIndexAndValue>();
public bool Add(string Key, int subIndex, string Value)
{
return true;
}
如您所见,Add 方法将接收 Key、Sub Key 和 Value。
将来我需要做一个 CRUD 并对数据进行排序。
我的问题是,我该如何处理?我如何使用 Contains() 之类的方法来检查一个 Value (字符串)是否已经使用此数据结构插入到字典中?或者也许有更简单的方法来做到这一点。
谢谢。
【问题讨论】:
-
另一种可能的方式
Dictionary<string,Dictionary<int,string>> -
覆盖您要作为字典键的类中的 Equals 和 GetHashCode 方法。另外,值得深思 - 为什么不能将 Key_SubKey 字符串作为键,将 List
作为值。 -
@PrateekShrivastava 是的,Key_SubKey 在某些方面可能不那么大惊小怪
-
@MichaelRandall - :) 很高兴收到您的来信,一二一。希望您在这些疯狂的时期保持健康并备货。
-
@PrateekShrivastava 囤积了咖啡和 resharper 订阅,但在世界末日中幸存下来
标签: c# class dictionary data-structures contains