Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
如何调用此linq查询?_Linq_C# 4.0_Delegates - Fatal编程技术网

如何调用此linq查询?

如何调用此linq查询?,linq,c#-4.0,delegates,Linq,C# 4.0,Delegates,假设我有一个返回字典的方法,如下所示: private Dictionary<string, int> GetWorktypeDictionaryList() { return new Dictionary<string, int>() { {"Select",-1},{"New MSC",1},{"Expansion",2},{"Migration",3},{"U

假设我有一个返回字典的方法,如下所示:

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}

                   };
    }
私有字典GetWorktypeDictionaryList()
{
返回新字典()
{
{“选择”、-1}、{“新建MSC”、1}、{“扩展”、2}、{“迁移”、3}、{“升级”、4}、{“重新定位”、5}、{“功能”、6}
};
}
现在在main方法中,我想用前面的字典值以小写形式添加字典键。为此,我使用Action委托。代码如下:

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));
            }
Dictionary dict=new Dictionary();
行动=
act=>dict.Add(act.Key.ToLower(),act.Value);
foreach(GetWorktypeDictionaryList()中的KeyValuePair对)
{
动作(新的KeyValuePair(pair.Key,pair.Value));
}
但我想用下面的两行代码实现这3行代码

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.
Dictionary dict=new Dictionary();
GetWorktypeDictionaryList().ToList().ForEach(act=>dict.Add(act.Key.ToLower(),act.Value))//下面是如何调用操作的问题。

但我无法在这些场景中调用操作。请我的一些高级程序员朋友帮我做这件事。

你可以使用
ToDictionary()


我不确定你的
行动在第二个示例中嵌入后如何发挥作用。

很好,满足我的工作要求。谢谢Stanley。但你能告诉我,在我尝试的最后一种方式中是否有可能。只是为了增加知识,其他什么都没有。行动只是尝试其他方式,其他什么都没有。
dict = GetWorktypeDictionaryList()
           .ToDictionary(kvp => kvp.Key.ToLower(), kvp => kvp.Value);