c# Dictionary 遍历,获取key、value


获取value
foreach (Student ss in stu.Values)
{

MessageBox.Show(ss.Name.ToString()); 
}

***********************************************

获取keyforeach (string key in stu.Keys)


MessageBox.Show(key);

}
****************

获取key、value
foreach (KeyValuePair a in cl)
            {
                if (a.Key == s1.Name)
                MessageBox.Show(a.Value.Age.ToString() + "  " + a.Value.Name.ToString ());
            }
****************

连个Dictionary是否相同:

foreach (var kvp in DictionaryA) 
{     
int value;     
if (DictionaryB.TryGetValue(kvp.Key, out value))    
 {        
 if (kvp.Value != value)    
     {         
    hasDictionaryChanged = true;          
   break;     
    }   
  }} 
****************

using System.Linq;

Dictionary students;

  var values = from u in students     
           let temp = u.Value.Scores.Sum()  orderby temp
              select new { 
name = u.Value.Name, totalscore = temp 
};
   ///显示查询结果
var dicSort = from d in letterWordTotalNum                                   
orderby d.Key
select d;

***********************************************

转换为数组:

Dictionary dic = new Dictionary();

object[] r = (from val in dic select val.Value).ToArray();




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2011/08/24/2151564.html,如需转载请自行联系原作者

你可能感兴趣的:(c# Dictionary 遍历,获取key、value)