【问题标题】:Dictionary using a class as Parameter and Key, CRUD C#使用类作为参数和键的字典,CRUD C#
【发布时间】:2020-07-01 01:51:50
【问题描述】:

所以我需要用one Keysub KeyValue创建一个数据结构。以及添加键、子键和值的方法。

唯一的,可以有多个 子键

子键内的上下文中是唯一的,可以有多个

子键的上下文中是唯一的

我想到的第一个数据结构是字典(执行时间对于任务很重要)。 然后我创建了以下字典。

    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 方法将接收 KeySub KeyValue

将来我需要做一个 CRUD 并对数据进行排序。

我的问题是,我该如何处理?我如何使用 Contains() 之类的方法来检查一个 Value (字符串)是否已经使用此数据结构插入到字典中?或者也许有更简单的方法来做到这一点。

谢谢。

【问题讨论】:

  • 另一种可能的方式Dictionary&lt;string,Dictionary&lt;int,string&gt;&gt;
  • 覆盖您要作为字典键的类中的 Equals 和 GetHashCode 方法。另外,值得深思 - 为什么不能将 Key_SubKey 字符串作为键,将 List 作为值。
  • @PrateekShrivastava 是的,Key_SubKey 在某些方面可能不那么大惊小怪
  • @MichaelRandall - :) 很高兴收到您的来信,一二一。希望您在这些疯狂的时期保持健康并备货。
  • @PrateekShrivastava 囤积了咖啡和 resharper 订阅,但在世界末日中幸存下来

标签: c# class dictionary data-structures contains


【解决方案1】:

我认为内置结构的组合可以解决您提出的数据结构:

Dictionary<string,Dictionary<int,HashSet<string>>>

第一个字典包含stringKey)的键值对列表,对于其中的每一个,值都是另一个字典,其键是唯一的string(SubKey),其值为HashSet&lt;string&gt;,一个唯一字符串列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 2013-05-04
    相关资源
    最近更新 更多