Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字典<;字符串,字符串>;到字典<;控制,对象>;使用IEnumerable<;T>;。选择()_C#_Linq_Dictionary_Lambda_Ienumerable - Fatal编程技术网

C# 字典<;字符串,字符串>;到字典<;控制,对象>;使用IEnumerable<;T>;。选择()

C# 字典<;字符串,字符串>;到字典<;控制,对象>;使用IEnumerable<;T>;。选择(),c#,linq,dictionary,lambda,ienumerable,C#,Linq,Dictionary,Lambda,Ienumerable,我有一个System.Collections.Generic.Dictionary,包含控件ID和适当的数据列以进行数据绑定: var dic = new Dictionary<string, string> { { "Label1", "FooCount" }, { "Label2", "BarCount" } }; var dic=新字典 { {“Label1”,“FooCount”}, {“Label2”,“BarCount”} }; 我是这样用的: pro

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

var dic = new Dictionary<string, string>
{
    { "Label1", "FooCount" },
    { "Label2", "BarCount" }
};
var dic=新字典
{
{“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]);
}
受保护的void FormView1\u数据绑定(对象发送方,事件参数e)
{
变量行=((DataRowView)FormView1.DataItem).row;
字典newOne=dic.ToDictionary(
k=>FormView1.FindControl(k.Key)),
k=>行[k.值];
}
所以我使用的是
IEnumerable.ToDictionary(Func,Func)


是否可以使用
IEnumerable.Select(Func)

当然可以,但返回值将是
IEnumerable
而不是
字典

IEnumerable newOne=dic。选择(
k=>新的KeyValuePair(FormView1.FindControl(k.Key),
行[k.值]);

(未经测试)

我不知道该怎么做?什么
Func
可以做到这一点?@abatishchev:更新了我的答案。
IEnumerable<KeyValuePair<Control, object>> newOne = dic.Select(
    k => new KeyValuePair<Control, object>(FormView1.FindControl(k.Key), 
                                           row[k.Value]));