gpt4 book ai didi

c# - Dictionary 到 Dictionary 使用 IEnumerable.Select()

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:18 37 4
gpt4 key购买 nike

我有一个 System.Collections.Generic.Dictionary<string, string>包含控件 ID 和适当的数据列以进行数据绑定(bind):

var dic = new Dictionary<string, string>
{
{ "Label1", "FooCount" },
{ "Label2", "BarCount" }
};

我是这样用的:

protected void FormView1_DataBound(object sender, EventArgs e)
{
var row = ((DataRowView)FormView1.DataItem).Row;
Dictionary<Control, object> newOne = dic.ToDictionary(
k => FormView1.FindControl(k.Key)),
k => row[k.Value]);
}

所以我正在使用 IEnumerable<T>.ToDictionary(Func<T>, Func<T>) .

是否可以使用 IEnumerable<T>.Select(Func<T>) 来做同样的事情? ?

最佳答案

当然可以,但返回值将是 IEnumerable<KeyValuePair<Control, object>>而不是 Dictionary<Control, object> :

IEnumerable<KeyValuePair<Control, object>> newOne = dic.Select(
k => new KeyValuePair<Control, object>(FormView1.FindControl(k.Key),
row[k.Value]));

(未经测试)

关于c# - Dictionary<string,string> 到 Dictionary<Control,object> 使用 IEnumerable<T>.Select(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2823626/

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