我想在字典中定义如下结构:
Dictionary<int, <int, string>>, 以第一个int为key,<int, string>为value,
请问如何实现。此外,
对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
如果这个value存在的话,我想找到它对应的key值,也就是那个int,
请问如何实现呢?给些思路,多谢了!

解决方案 »

  1.   

    这样效率不高,你不如不要用Dictionary了。自己创建一个这样的结构。
      

  2.   

    Dictionary<int,T> dic = new Dictionary<int,T>(); 
    div.Add(1,new T(1,"1"));
    class T
    {
     public int id{get;set;}
     public string title{get;set;}
    }
      

  3.   

    foreach (KeyValuePair<string, T> item in dic )
    {
                 
    }
      

  4.   

    Dictionary<int, Dictionary<int, string>>
      

  5.   

    Dictionary<int,T> dic = new Dictionary<int,T>();  
    div.Add(1,new T(1,"1"));
    class T
    {
     public int id{get;set;}
     public string title{get;set;}
    }
      

  6.   

    多谢了!
    那么第二个问题呢?
    对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
    如果这个value存在的话,我想找到它对应的key值,也就是那个int,
    请问如何实现呢?
    =========
    自己编程遍历?
      

  7.   

    关键是这个value会出现重复否,只取第一个遍历到然后bresk 或者return 就行了,多个值的话整个List也可以的
      

  8.   


           public object GetKey(string str)
            {
                foreach (KeyValuePair<int, string> kp in  dic )
                {
                    if (kp.Value != "")
                    {
                        return kp.Key;
                    }
                }
                return null;
            }
      

  9.   

    Dictionary<int,String> dic = new Dictionary<int,String>();   
    if(dic.Keys[key]!=null)
    {
    //存在key键对应的值
    }
    else
    {
    //不存在key键对应的值
    }
      

  10.   

    第二个问题比较怪,一般字典类是保证key肯定是唯一的,现在知道value,去找Key,你能保证value唯一么?
    例如这样的例子可能就麻烦了,
    Dictionary<int,T> dic = new Dictionary<int,T>();   
    dic.Add(1,T1);
    dic.Add(2,T1);如果你找T1的Key,遍历的话,就有2个。
      

  11.   

    为什么效率不高,Readability 比较差是真的。
    Dictionary<int, KeyValuePair<int, string>> dict = ...;
    var keys = dict.Where(n => n.value.key == xxx && n.value.value == yyy);
    var key = keys.FirstOrDefault();
      

  12.   

    多谢,我基本上就是按照你这种方法写的。var key = (from k in mydic where string.Compare(k.Value, myString, true) == 0 select k.Key).FirstOrDefault();