我有以下声明:
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>().Where(three => three.Name.Contains("Three")))
我是如何做到这一点的,以便我可以做到where name contain three or four
?
请帮忙。感谢
答案 0 :(得分:5)
使用或||
运算符组合where子句中的条件。
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>()
.Where(three => three.Name.Contains("Three") || three.Name.Contains("Four")))
答案 1 :(得分:3)
string[] names = { "Three", "Four" } ;
foreach (var textBlock in scoresGrid.Children.OfType<TextBlock>()
.Where(tb => names.Any(name => tb.Name.Contains(name))))
另外,我认为你的foreach陈述有点难以阅读。首先获取过滤后的textBlocks。然后迭代它们。
答案 2 :(得分:1)
您可以使用逻辑运算符。对于或它是'||'
>().Where(three => three.Name.Contains("Three") || three.Name.Contains("Four")))