gpt4 book ai didi

c# - 如何附加到我的 KeyValuePair?

转载 作者:行者123 更新时间:2023-11-30 21:09:20 25 4
gpt4 key购买 nike

我的 list返回 <string, string>显然。如何在我的 KeyValuePair 中附加第二个字符串并添加我从第二次数据库调用返回的 ID?

    // 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;

最佳答案

KeyValuePair<TKey, TValue>键和值是只读的。你可以使用 Dictionary<string,string>为你工作并获得KeyValuePair<string, string>从它。

我认为这行得通:

// 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;

要附加第二个字符串,您可以使用 list["/*Your ID*/"]= YourData;

List<KeyValuePair<TKey, TValue>>不是好办法。就像你用一杯茶来灭火一样!

关于c# - 如何附加到我的 KeyValuePair?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com