Humayoun_Kabir
Humayoun_Kabir

Reputation: 2251

How to Invoke this linq query?

Suppose i have a method that return a dictionary as below :

private Dictionary<string, int> GetWorktypeDictionaryList()
    {
        return new Dictionary<string, int>()
                   {
                       {"Select",-1},{"New MSC",1},{"Expansion",2},{"Migration",3},{"Upgrade",4},{"Relocation",5},{"Feature",6}

                   };
    }

now in main method i want to add dictionary key in lower case with previous dictionary value.For these purpose i use Action delegate. code are below :

Dictionary<string,int> dict = new Dictionary<string, int>();

            Action<KeyValuePair<string, int>> action =
                act => dict.Add(act.Key.ToLower(), act.Value);

            foreach (KeyValuePair<string,int> pair in GetWorktypeDictionaryList())
            {
                action(new KeyValuePair<string, int>(pair.Key,pair.Value));
            }

but i want to implement these 3 lines of code in 2 lines like below.

Dictionary<string,int> dict = new Dictionary<string, int>();
GetWorktypeDictionaryList().ToList().ForEach(act => dict.Add(act.Key.ToLower(), act.Value));//Here is the problem how to invoke Action.

but i couldn't invoke Action in these scenario . Please Some of my advanced programmer friends help me to do this.

Upvotes: 0

Views: 36

Answers (1)

D Stanley
D Stanley

Reputation: 152566

You can just use ToDictionary():

dict = GetWorktypeDictionaryList()
           .ToDictionary(kvp => kvp.Key.ToLower(), kvp => kvp.Value);

I'm not sure how your Action comes in to play when you've embedded it in the second example.

Upvotes: 1

Related Questions