带where子句的foreach循环 [英] foreach loop with a where clause

查看:169
本文介绍了带where子句的foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在C#中使用where循环创建一个foreach循环.不是在其中包含if语句,而是在声明循环时使用and子句.

I was wondering if it's possible to create a foreach loop in C# with a where loop. Not with a if statement inside, but and where clause in the declaring of the loop.

也许是这样吗?

foreach(var n in people where n.sex == male)
{
}

推荐答案

是的,有可能:

方法语法:

foreach (var person in people.Where(n => n.sex == "male"))
{
}

或者相当冗长的查询语法:

Or the rather lengthy Query Syntax:

foreach (var person in from person in people where person.sex == "male" select person) 

这篇关于带where子句的foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆