C# 如何跳过 Foreach 中的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/969796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to skip a record in a Foreach
提问by Polo
I am trying to create an object from an Active Directory base on a simple login. The problem is that some of the login information is valid.
我正在尝试基于简单的登录从 Active Directory 创建一个对象。问题是一些登录信息是有效的。
How could I just use a try-catch so that if an exception is thrown, just skip to the next login?
我怎么能只使用 try-catch 以便在抛出异常时跳到下一次登录?
Here is the code:
这是代码:
foreach (var PharosUserItem in ListRef)
{
ADUser User;
try
{
User = new ADUser(PharosUserItem.UserLoginPharos);
}
catch (ByTel.DirectoryServices.Exceptions.UserNotFoundException ex)
{
break;
}
}
The break gets me out of the foreach
, which is not what I want. Any ideas?
休息让我摆脱了foreach
,这不是我想要的。有任何想法吗?
采纳答案by Joseph
You are looking for continue
您正在寻找继续
foreach (var PharosUserItem in ListRef)
{
ADUser User;
try
{
User = new ADUser(PharosUserItem.UserLoginPharos);
}
catch (ByTel.DirectoryServices.Exceptions.UserNotFoundException ex)
{
continue;
}
}
回答by Jon Skeet
Use the continue
statement instead:
请改用以下continue
语句:
foreach (var pharosUserItem in ListRef)
{
ADUser user;
try
{
user = new ADUser(pharosUserItem.UserLoginPharos);
}
catch (UserNotFoundException)
{
continue;
}
// Use "user" here
}
(I've made a few changes in terms of variable casing, avoiding using a massively long fully-qualified name for the exception, and providing a variable for the exception which you then ignore.)
(我在变量大小写方面做了一些更改,避免为异常使用非常长的完全限定名称,并为异常提供一个变量,然后您将其忽略。)
Note that if there's any reasonable way that you could get a list of valid users and check it, that would be nicer than using the exception for flow control as you're doing here. It may not be feasible, but it's worth checking :)
请注意,如果有任何合理的方法可以获得有效用户的列表并对其进行检查,那会比在此处使用异常进行流控制更好。这可能不可行,但值得检查:)
回答by tanascius
You have to use continue;
你必须使用 continue;
回答by TheTXI
Use the continue
statement to skip to the next iteration.
使用continue
语句跳到下一次迭代。
回答by Simon P Stevens
Rather than throwing an exception, instead you should try and check if the user is valid first. Throwing exceptions is quite expensive and should really only be used in 'exceptional' circumstances and not to control the logical flow of the app, this isn't what they should be used for as you are expecting some users to fail.
与其抛出异常,不如先尝试检查用户是否有效。抛出异常非常昂贵,应该只在“异常”情况下使用,而不是控制应用程序的逻辑流程,这不是它们应该用于的目的,因为您预计某些用户会失败。
回答by Crash893
why not use nothing instead of continue?
为什么不使用什么而不是继续?
Use the continue statement instead:
请改用 continue 语句:
foreach (var pharosUserItem in ListRef)
{
ADUser user;
try
{
user = new ADUser(pharosUserItem.UserLoginPharos);
}
catch (UserNotFoundException)
{
}
// Use "user" here
}
or put a
或放一个
console.writeline("user not found: "+ pharosuseritem.tostring() );