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

时间:2010-01-12 18:54:54

标签: c# .net-2.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(allObjects中的DataModelObject dmo)” (dmo是IResourcePolicy),if(string.Compare(dmo.Name,hostName,true)== 0)“,,      如果执行这些循环,那么我想执行整个四个赋值 在循环内部包括前一个赋值(irp.AgentVersion = agentVersion;)也。      以前它在UI中没有显示没有执行循环,一旦执行 显示我们需要更改的所有值

任何人都可以给代码执行这个逻辑,那里有“Goto”循环条件检查我们可以在这里做什么

5 个答案:

答案 0 :(得分:4)

我相信您正在寻找continue

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);
    }
}

答案 1 :(得分:2)

要破译你正在寻找的东西有点困难,但我会抓住它:

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"
}

答案 2 :(得分:1)

您可以使用一些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)
}

答案 3 :(得分:1)

如果我拥有.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);
}

答案 4 :(得分:-1)

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

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; 
    }
}