如何跳过循环条件来获取功能
一些代码
foreach (DataModelObject dmo in allObjects)
{
if (dmo is IResourcePolicy)
{
if (string.Compare(dmo.Name, hostName, true) == 0)
{
IResourcePolicy irp = (IResourcePolicy)dmo;
irp.ResourcePolicy = rp;
irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
irp.AgentVersion = agentVersion;
// Distribute the object without saving it.
SpoServer.Spurt.ServerSendObject(dmo, true, 0);
break;
}
}
}
这是我想要执行此语句“irp.AgentVersion = agentVersion;”的 不执行这三个循环“ foreach (DataModelObject dmo in allObjects),if (dmo 是 IResourcePolicy), if (string.Compare(dmo.Name, hostName, true) == 0)",, 如果执行这些循环,那么我想执行整个四个作业 循环内部还包括之前的赋值(irp.AgentVersion = agentVersion;)。 以前,在不执行循环的情况下,它在 UI 中不显示任何内容,一旦执行 显示我们需要更改的所有值,
任何人都可以提供执行此逻辑的代码吗,我们可以在这里执行“Goto”循环条件检查
Here is a bit of code
foreach (DataModelObject dmo in allObjects)
{
if (dmo is IResourcePolicy)
{
if (string.Compare(dmo.Name, hostName, true) == 0)
{
IResourcePolicy irp = (IResourcePolicy)dmo;
irp.ResourcePolicy = rp;
irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
irp.AgentVersion = agentVersion;
// Distribute the object without saving it.
SpoServer.Spurt.ServerSendObject(dmo, true, 0);
break;
}
}
}
i want to get executed this statement "irp.AgentVersion = agentVersion;" without executing these three loops " foreach (DataModelObject dmo in allObjects),if
(dmo is IResourcePolicy), if (string.Compare(dmo.Name, hostName, true) == 0)",,
if these loops are executed then i want to execute the entire four assignment
inside the loop including the previous assignment(irp.AgentVersion = agentVersion;) also.
Previously its showing none in UI without executing loop,,once executed
showing all values,, that we need to change
Can anyone give the code to execute this logic,,IS there "Goto" loop condition checking we can do here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信您正在寻找继续。
编辑:
根据评论,我理解您想要做的事情如下:
另请注意,这里只有一个循环,一旦满足您的内部条件,您就会打破它。我想这可能是让你感到困惑的地方。现在的情况是,您将始终只处理集合中的一个对象。
下面删除了break语句,因此它将处理集合中的每个对象。
I believe you are looking for continue.
EDIT:
Based on the comments, here is what I understand you want to do:
Also to note, there is only one loop here, and you're breaking out of it once your inner conditional is met. I think this may be what is confusing you. The way it is now, you'll always be processing only one of the objects in your collection.
The following has the break statement removed so it will process every object in your collection.
破译你要找的东西有点困难,但我会尝试一下:
It's a little difficult to decipher what you're looking for, but I'll take a stab at it:
您可以使用一些 LINQ 来消除循环和嵌套 if 语句。总体思路如下:
You can eliminate your loop and nested if statements by using some LINQ. Here's the general idea:
如果我有 .NET 3.5 或更好的版本,我将如何重写该代码。没有循环,只有一个
if
语句。Here's how I would rewrite that code, if I had the luxury of .NET 3.5 or better. No loops, one
if
statement.我不清楚你想做什么。这很接近吗?
I am not clear on what you are trying to do. Is this close?