How do I get previous key from SortedDictionary?

后端 未结 7 2244
误落风尘
误落风尘 2020-12-18 20:46

I have dictionary containing key value pairs.

SortedDictionary dictionary=new SortedDictionary();
dictionary.Add(1,33);
diction         


        
7条回答
  •  青春惊慌失措
    2020-12-18 20:55

    I would prefer to use linq if that's the case... try this it will surely work

    SortedDictionary dictionary = new SortedDictionary();
    dictionary.add(1, 33);
    dictionary.add(2, 20);
    dictionary.add(4, 35);
    
    int SelectedKey = 4;
    
    var ResutValue = ( from n in dictionary    
                   where n.Key < TheSelectedKey
                   select n.Value).Last();
    
    this.txtResult.Text = ResultValue.ToString();
    

提交回复
热议问题