2012-01-30 15 views
2

私のlistは、明らかに<string, string>を返します。私のKeyValuePairに2番目の文字列を追加し、2番目のデータベース呼び出しから取得したIDをどのように追加できますか?KeyValuePairにどのように追加できますか?

// Database calls here 

    KeyValuePair<string, string> parks = new KeyValuePair<string, string>(); 

    DataSet dset = new DataSet(); 

    var list = new List<KeyValuePair<string, string>>(); 

    list.Add(new KeyValuePair<string, string>("Select one", "0")); 
    foreach (DataRow row in ds.Tables[0].Rows) 
    { 
     //My database calls including: 
     db.AddInParameter(comm, "@pID", DBType.Int32, row[1]); 
     dset = db.ExecuteDataSet(comm); 
     string attr = dset.Tables[0].Rows[0]["Value"].ToString(); 
     list.Add(new KeyValuePair<string, string>(row[0].ToString() + " - " + attr, row[1].ToString())); 

    } 

    return list; 

答えて

2

KeyValuePair<TKey, TValue>キーと値は読み取り専用です。あなたは仕事のためにDictionary<string,string>を使うことができ、それからKeyValuePair<string, string>を得ることができます。

私はこれがうまくいくと思う:あなたはlist["/*Your ID*/"]= YourData;

List<KeyValuePair<TKey, TValue>>は良い方法ではありません使用することができます

// Database calls here 
Dictionary<string, string> list = new Dictionary<string, string>(); 

list.Add("Select one", "0"); 
foreach (DataRow row in ds.Tables[0].Rows) 
{ 

    list.Add(row[0].ToString(), row[1].ToString()); 

} 

//Another database call that gets back an ID. I want to add/append this ID into my second string in my KeyValue Pair. 

return list; 

2番目の文字列を追加するために。それは、あなたが火を吹き飛ばすために紅茶を使用するようなものです!

+0

私はこれを試してみましょう。 –

+0

@TCC:Dictionaryあなたのような問題の解決法です。 –

+0

@TCC:どうしたの? –

2

KeyValuePair<TKey, TValue>は不変です。 KeyValuePair<string, string>を使用する場合は、変更するペアを削除し、変更された値で新しいペアを追加する必要があります。

また、別のデータ型を使用してください。値を変更する場合は、値を変更できる型を使用します。

+0

私のコードで削除して実装する方法の例はありますか? –

関連する問題