简体   繁体   English

C#如果在foreach中使用

[英]C# Use if in foreach

The Code below resets form labels to "0". 下面的代码将表单标签重置为“ 0”。 Out of curiosity, I am trying to figure out how to access only certain items rather than every item in the List. 出于好奇,我试图找出如何仅访问某些项而不访问列表中的每个项的方法。 The foreach function works great here but in other parts of the code I would like to access and modify these objects diferently foreach函数在这里很好用,但是在代码的其他部分,我想分别访问和修改这些对象

For example how could I set every other field to "0" or set only the last or first three to some value, etc. 例如,如何将每个其他字段设置为“ 0”,或者仅将最后三个或前三个设置为某个值,等等。

// Set the form Labels to "0"
private void btnClear_Click(object sender, EventArgs e)
{
    // Create List of Labels for modifying form
    new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 }.ForEach(p => p.Text = "0");
}

Thank You 谢谢

Update: 更新:

Well after lots of reading and testing I came back to the most basic method for selecting every other item in the list. 经过大量的阅读和测试之后,我回到了选择列表中其他所有项目的最基本方法。

        // Set the form Labels to "0"
    private void btnClear_Click(object sender, EventArgs e)
    {
        // Create List of Labels for modifying form
        List<Label> lbl = new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 };
        int x =1; //  Set to get every other label. 0 = even, 1 = odd
        while (x < lbl.Count)
        {
            lbl[x].Text = "0";
            x = x + 2;
        }

@Jonathan Wood is there a method to this with the items.ForEach(p => { some code(), p.Text = "0"; }); @Jonathan Wood是否有使用此方法的方法items.ForEach(p => { some code(), p.Text = "0"; });

The delegate that you pass to ForEach() can be enclosed in curly braces, which allows you to define more complex logic such as if statements. 传递给ForEach()的委托可以用花括号括起来,这使您可以定义更复杂的逻辑,例如if语句。

items.ForEach(p => { if (p != null) p.Text = "0"; });

You can even define your delegate as a separate method and just pass that. 您甚至可以将您的委托定义为单独的方法,然后将其传递。

However, I wonder about your approach. 但是,我想知道您的方法。 Is this a winforms app? 这是Winforms应用程序吗? Why not just do a regular loop that loops through controls on the form? 为什么不只执行循环遍历表单控件的常规循环呢? Then the additional table won't be necessary. 这样就不需要附加表了。

To set every other field to "0" you could try the IEnumerable.Where overload that passes the index of current element to the predicate. 要将所有其他字段设置为“ 0”,可以尝试IEnumerable.Where重载,该重载会将当前元素的索引传递给谓词。

Let's say you want to work on the elements with odd index: 假设您要处理具有奇数索引的元素:

new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 }.Where((p, i) => (i & 1) == 1).ToList().ForEach(p => p.Text = "0");

You can use Linq: 您可以使用Linq:

first three: 前三个:

var labels = new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 };
foreach(Label lbl in labels.Take(3))
    lbl.Text = someValue;

last three: 最后三个:

foreach(Label lbl in labels.Reverse().Take(3))
    lbl.Text = someValue;

other labels: 其他标签:

var allLabels = this.Controls.OfType<Label>();
var otherLabels = allLabels.Except(labels);
foreach(Label lbl in otherLabels)
    lbl.Text = "0";

Set first 3 to 0 将前3设为0

new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 }.Take(3).ForEach(p => p.Text = "0");

Set last 3 to 0 将最后3设为0

new List<Label>() { lbl1, lbl2, lbl3, lbl4, lbl5, lbl6 }.Skip(3).Take(3).ForEach(p => p.Text = "0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM