在我的代码中,“ sourceElements”是一种
List<KeyValuePair<string, string>>.
我需要查询此列表的键是否包含特定值,并且尝试了此操作:
sourceElements.Add(new KeyValuePair<string, string>("t","t"));
sourceElements.Add(new KeyValuePair<string, string>("test", "test"));
sourceElements.Add(new KeyValuePair<string, string>("t1", "t2"));
if (sourceElements.All(x => x.Key.Contains("test", StringComparer.InvariantCultureIgnoreCase))
{
// do some stuff here
}
但是编译器报告“无法从用法中推断出类型自变量”。
有什么想法在代码中不正确的地方吗?
该代码应该可以使用(在LINQPad中不给出错误)
List<KeyValuePair<string, string>> sourceElements = new List<KeyValuePair<string, string>>();
sourceElements.Add(new KeyValuePair<string, string>("t","t"));
sourceElements.Add(new KeyValuePair<string, string>("test", "test"));
sourceElements.Add(new KeyValuePair<string, string>("t1", "t2"));
if (sourceElements.All(x => x.Key.ToLowerInvariant().Contains("test")))
{
// do some stuff here
}
因此,如果您用t和t1注释掉键,则if
-block内的代码将执行
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句