使用类作为参数和关键字的字典,CRUD C#

问题描述 投票:0回答:1

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

<< [Key是unique,并且可以具有multiple Sub Keys

Sub Key

Key内部的上下文中为uniques,并且可以具有多个Values

Values

Sub Key的上下文中是unique我想到的第一个数据结构是

字典

(执行时间对任务很重要)。然后我创建了以下字典。 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

(字符串)?也许有一种更简单的方法可以做到这一点。谢谢。
c# class dictionary data-structures contains
1个回答
0
投票
我认为内置结构的组合可以解决您建议的数据结构:

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

第一个字典包含string

Key)的键-值对的列表,对于每个键-值对,该值都是另一个字典,其键是唯一的stringSubKey ),值是HashSet<string>,这是唯一字符串的列表。

© www.soinside.com 2019 - 2024. All rights reserved.