如何跳过循环条件来获取功能

发布于 2024-08-17 14:14:46 字数 1007 浏览 4 评论 0原文

一些代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱情眠于流年 2024-08-24 14:14:46

我相信您正在寻找继续

if (dmo is IResourcePolicy)
{
    etc...
}
else
{
    continue;
}

编辑:

根据评论,我理解您想要做的事情如下:

另请注意,这里只有一个循环,一旦满足您的内部条件,您就会打破它。我想这可能是让你感到困惑的地方。现在的情况是,您将始终只处理集合中的一个对象。

下面删除了break语句,因此它将处理集合中的每个对象。

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy)
    {
         // if these loops are not executed i want to show agentversion instead of showing None in UI layer
         IResourcePolicy irp = (IResourcePolicy)dmo;
         irp.AgentVersion = agentVersion;

         //(else) i want to show the entire four things including agent version
         if (string.Compare(dmo.Name, hostName, true) == 0)
         {             
             irp.ResourcePolicy = rp;
             irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
             irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
         }

         // Distribute the object without saving it.
         SpoServer.Spurt.ServerSendObject(dmo, true, 0);
    }
}

I believe you are looking for continue.

if (dmo is IResourcePolicy)
{
    etc...
}
else
{
    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.

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy)
    {
         // if these loops are not executed i want to show agentversion instead of showing None in UI layer
         IResourcePolicy irp = (IResourcePolicy)dmo;
         irp.AgentVersion = agentVersion;

         //(else) i want to show the entire four things including agent version
         if (string.Compare(dmo.Name, hostName, true) == 0)
         {             
             irp.ResourcePolicy = rp;
             irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
             irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
         }

         // Distribute the object without saving it.
         SpoServer.Spurt.ServerSendObject(dmo, true, 0);
    }
}
尴尬癌患者 2024-08-24 14:14:46

破译你要找的东西有点困难,但我会尝试一下:

bool objectsFound = false;
foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)
    {
        // ...
        objectsFound = true;
    }
}

if(objectsFound)
{
    // "show the entire four things including agent version"
}
else
{
    // " show agentversion instead of showing None in UI layer"
}

It's a little difficult to decipher what you're looking for, but I'll take a stab at it:

bool objectsFound = false;
foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)
    {
        // ...
        objectsFound = true;
    }
}

if(objectsFound)
{
    // "show the entire four things including agent version"
}
else
{
    // " show agentversion instead of showing None in UI layer"
}
守望孤独 2024-08-24 14:14:46

您可以使用一些 LINQ 来消除循环和嵌套 if 语句。总体思路如下:

var objects = new List<Object>();
objects.Add(1);
objects.Add("string");
objects.Add("magic");
objects.Add(2.5);

var magic = (from o in objects
             where o is string
                && ((string)o) == "magic"
             select o as string).SingleOrDefault();

if(magic != null) {
    Console.Write("magic found: {0}", magic);
}
else {
    // Do your other logic if nothing was found (loop, etc)
}

You can eliminate your loop and nested if statements by using some LINQ. Here's the general idea:

var objects = new List<Object>();
objects.Add(1);
objects.Add("string");
objects.Add("magic");
objects.Add(2.5);

var magic = (from o in objects
             where o is string
                && ((string)o) == "magic"
             select o as string).SingleOrDefault();

if(magic != null) {
    Console.Write("magic found: {0}", magic);
}
else {
    // Do your other logic if nothing was found (loop, etc)
}
别靠近我心 2024-08-24 14:14:46

如果我有 .NET 3.5 或更好的版本,我将如何重写该代码。没有循环,只有一个 if 语句。

var irp = allObjects.OfType<IResourcePolicy>()
    .FirstOrDefault(item => String.Equals(item.Name, hostName));

if (irp != null)
{
     irp.ResourcePolicy = rp;
     irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
     irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
     irp.AgentVersion = agentVersion;

     // I don't know the signature of ServerSendObject, 
     // you might need a cast here:
     SpoServer.Spurt.ServerSendObject(irp, true, 0);
}

Here's how I would rewrite that code, if I had the luxury of .NET 3.5 or better. No loops, one if statement.

var irp = allObjects.OfType<IResourcePolicy>()
    .FirstOrDefault(item => String.Equals(item.Name, hostName));

if (irp != null)
{
     irp.ResourcePolicy = rp;
     irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
     irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
     irp.AgentVersion = agentVersion;

     // I don't know the signature of ServerSendObject, 
     // you might need a cast here:
     SpoServer.Spurt.ServerSendObject(irp, true, 0);
}
玩心态 2024-08-24 14:14:46

我不清楚你想做什么。这很接近吗?

foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy)
    {
         IResourcePolicy irp = (IResourcePolicy)dmo; 
         irp.AgentVersion = agentVersion; 

         if (string.Compare(dmo.Name, hostName, true) == 0) 
         {
            irp.ResourcePolicy = rp; 
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
         }

         // Distribute the object without saving it. 
         SpoServer.Spurt.ServerSendObject(dmo, true, 0); 

         break; 
    }
}

I am not clear on what you are trying to do. Is this close?

foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy)
    {
         IResourcePolicy irp = (IResourcePolicy)dmo; 
         irp.AgentVersion = agentVersion; 

         if (string.Compare(dmo.Name, hostName, true) == 0) 
         {
            irp.ResourcePolicy = rp; 
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
         }

         // Distribute the object without saving it. 
         SpoServer.Spurt.ServerSendObject(dmo, true, 0); 

         break; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文