日期:2014-05-17  浏览次数:20864 次

Dictionary数据字典检索求解?
key value
01 类别甲
0101 类别乙
0102 类别丙
0103 ...
010301 ...
010302 ...
0104 ...
010401 ...
0105 ...
010501 ...
010502 ...
010503 ...
01050301 ...
02 ...
0201 ...
020101 ...
...
如上所示这些键值是有规律的,即存在父子关系。

这些键值是通过dictionary来存放,现在传递某个key值,来找出这个key值的所有父级类别,及下一级子类别。

例如给到0105这个key则要找出(且按顺序):
01 ...
0105 ...
010501 ...
010502 ...
010503 ...
请教各位达人,有什么“高效”的算法来查出想要的数据。

------解决方案--------------------
高效的不知道,不高效的有一个

C# code

 Dictionary<string, string> List = new Dictionary<string, string>();
            List.Add("01", "aaa");
            List.Add("0105", "aaa");
            List.Add("010501", "aaa");
            string str = "0105";
            Dictionary<string, string> ListB = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> kvp in List)
            { 
                if(kvp.Key==str)
                    continue;
                if (str.StartsWith(kvp.Key))
                {
                    ListB.Add(kvp.Key, kvp.Value);
                }
            }
            foreach (KeyValuePair<string, string> kvp in List)
            {
                if (kvp.Key == str)
                    continue;
                if (kvp.Key.StartsWith(str))
                {
                    ListB.Add(kvp.Key, kvp.Value);
                }
            }

------解决方案--------------------
上面的bss代码断开了,更正一下:
C# code
class 科目树
{
    Dictionary<string, 科目树> 下级子科目= new Dictionary<string, 科目树>;
    Entity 本级别的数据;
}