2013年9月17日 星期二

[轉貼] Dictionary的foreach使用KeyValuePair



最近剛好要用到Dictionary來記錄一些東西..
但不知道如何使用foreach把每一個Dictionary裡的東西抓出來..
去找了一下msdn,把它記在Blog裡...分享給大家呀..
找到了一些資訊:
foreach (KeyValuePair<TKey, TValue> kvp in myDictionary) {...}

Tkey,TValue可以是不同型態,不一定要是string
看到這句就大概可以猜出來了,就是用KeyValuePair,用範例介紹如何使用..
//注意,鍵相當於找到對應值的唯一標識,所以不能重複 
//但是值可以重複 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class TestKeyValuePair : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        if (!dic.ContainsKey("Name")) dic.Add("Name", "puma");
        if (!dic.ContainsKey("Blog")) dic.Add("Blog", "F6 Team");
        if (!dic.ContainsKey("Group")) dic.Add("Group", "Dotblogs");
        //進行排序
        var result = from pair in dic
                         orderby pair.Key
                         select pair;

        foreach (KeyValuePair<string, string> item in result)
        {
            Response.Write(string.Format("{0} : {1}
", item.Key, item.Value));
        }
    }
}
執行結果:
Blog : F6 Team Group : Dotblogs Name : puma
參考網址:
**************************************************************************************************************
自己之前寫的單位換算的部份程式碼,也有用到類似的語法,將文字檔內容對應到下拉選單
例:面積.txt 平方公尺(m^2),1 公畝(a.),0.01 公頃(ha.),0.0001 英畝(acre),0.000247106 日坪(台坪),0.3025 日畝(台畝),0.0100833 台灣甲,0.000103102 平方呎(ft^2),10.7639104167097 平方吋(in^2),1550.0031000062 平方碼(yard^2),1.19599004630108 平方哩(mile^2),3.86102158542446E-07
**************************************************************************************************************
        private void Form1_Load(object sender, EventArgs e)         {             SelectUnit("面積");         }
        //選取要進行換算的單位         void SelectUnit(string path)         {             // 設定 comboBox.Items 的初始值             ArrayList cboItems1 = new ArrayList();
            //讀取要換算的單位             string filename = @"data\" + path + ".txt";             StreamReader sr = new StreamReader(filename);             string line = sr.ReadLine();             while (line != null)             {                 string[] datas = line.Split(',');                 string key = datas[0];                 double value = double.Parse(datas[1]);
                cboItems1.Add(new KeyValuePair (key, value));
                line = sr.ReadLine();             }             sr.Close();
            //填值進 comboBox.Items             comboBox1.ValueMember = "Value";             comboBox1.DisplayMember = "Key";             comboBox1.DataSource = cboItems1;         }
****************************************************************************
另外的排序方式
// Create dictionary and add five keys and values.
 var dictionary = new Dictionary();
 dictionary.Add("car", 2);
 dictionary.Add("apple", 1);
 dictionary.Add("zebra", 0);
 dictionary.Add("mouse", 5);
 dictionary.Add("year", 3);

 // Acquire keys and sort them.
 var list = dictionary.Keys.ToList();
 list.Sort();

 // Loop through keys.
 foreach (var key in list)
 {
     Console.WriteLine("{0}: {1}", key, dictionary[key]);
 }
出處:http://www.dotnetperls.com/sort-dictionary

沒有留言:

張貼留言