views:

212

answers:

1

I have a System.Collections.Generic.Dictionary<string, string> containing control ID and appropriate data column to data bind:

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

I use it that way:

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'm using IEnumerable<T>.ToDictionary(Func<T>, Func<T>).

Is it possbile to do the same using IEnumerable<T>.Select(Func<T>) ?

+2  A: 

Sure, but the return value will be an IEnumerable<KeyValuePair<Control, object>> rather than a Dictionary<Control, object>:

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

(untested)

Heinzi
I can't figure out how to do that exactly? What `Func<T>` can do that?
abatishchev
@abatishchev: Updated my answer.
Heinzi