楼上回答的不太对,题目只说查找带“色”字的,没说以“色”结尾的
我的代码是这样
?var?openwith=?new?Dictionary<string,string>(); openwith.Add("red","红色"); openwith.Add("blue","蓝色"); openwith.Add("apple","苹果"); //查找所有目标项 var?resultItem?=?openwith.Where(c?=>?c.Value.Contains("色"));//循环输出结果
foreach?(var?keyValuePair?in?resultItem) { Console.WriteLine(keyValuePair.Key); } Console.ReadLine();附上截图:
以及额外的版本和截图:
var?openwith=?new?Dictionary<string,string>(); openwith.Add("red","红色"); openwith.Add("blue","蓝色"); openwith.Add("apple","苹果"); Console.WriteLine("请输入要查询的单词:"); var?words?=?Console.ReadLine(); while?(string.IsNullOrEmpty(words)) { Console.WriteLine("输入的字符为空,请重新输入:"); words?=?Console.ReadLine(); } //去除空格 words?=?words.Trim(); if?(openwith.Values.Any(c=>c.Contains(words))) { var?resultItem?=?openwith.Where(c?=>?c.Value.Contains(words)); Console.WriteLine("查找完成,包含{0}的字典的键为:",?words); foreach?(var?keyValuePair?in?resultItem) { Console.WriteLine(keyValuePair.Key); } } else { Console.WriteLine("查找完成,字典的值中不包含:{0}",?words); } Console.ReadLine();