我有以下声明:
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>().Where(three => three.Name.Contains("Three")))
我怎么能做到这样我才能做到呢where name contain three or four
?
请帮忙。谢谢
使用 or||
运算符组合 where 子句中的条件。
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>()
.Where(three => three.Name.Contains("Three") || three.Name.Contains("Four")))
string[] names = { "Three", "Four" } ;
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>()
.Where(tb => names.Any(name => tb.Name.Contains(name))))
另外,我认为您的 foreach 语句有点难以阅读。首先获取过滤后的文本块。然后迭代它们。
您可以使用逻辑运算符。对于或它是'||'
>().Where(three => three.Name.Contains("Three") || three.Name.Contains("Four")))