Dictionary <string, string> to dictionary <Control, object> using IEnumerable <T> .Select ()

I have a System.Collections.Generic.Dictionary<string, string>

containing control id and corresponding data column for data binding:

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

      

I use it like this:

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]);
}

      

So, I am using IEnumerable<T>.ToDictionary(Func<T>, Func<T>)

.

Is it possible to do the same with IEnumerable<T>.Select(Func<T>)

?

+2


a source to share


1 answer


Sure, but the return value will be IEnumerable<KeyValuePair<Control, object>>

, not Dictionary<Control, object>

:

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

      



(unverified)

+2


a source







All Articles