Dictionary<int, string> names= GetNames().Where(x =>
x.Value.StartsWith("test") | x.Value.StartsWith(" " + "test")).ToDictionary(x => x.Key, y => y.Value);
getNames() 方法的值是这样的:
约翰测试
测试特拉维斯
测试Metsaoy
使用上面的代码行,我只得到最后两个条目,但我还想要第一个,因为第二个字符串以“test”开头。
所以,我需要修改上面的where语句。我试过这样的事情:
.Where(x =>
foreach ( xx in x.Value.Split(' ') ) { if ( xx.StartsWith("text") ) true; })
我怎样才能做到这一点?
最佳答案
var res = GetNames().Where(kvp => kvp.Value.Split()
.Any(s => s.StartsWith("Test") || s.StartsWith("test")));
您可以选择使用 String.Contains
而不是 StartsWith
在 Any
lambda 中。
关于c# - 在 lambda 表达式中使用 substring 和 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34332081/