C# 过滤数组中的元素

C# 过滤数组中的元素,c#,wpf,arrays,filtering,C#,Wpf,Arrays,Filtering,我有一个数组 也就是说,每个项目在以下索引中都有其类别。 我需要分类为TotalNumber和CurrentNumber的所有项目 我试过了 int i = 1; foreach (string item in statsname) { //only number type stats are added to the comboboxes. if ((statsname[i].

我有一个数组

也就是说,每个项目在以下索引中都有其类别。 我需要分类为TotalNumber和CurrentNumber的所有项目

我试过了

 int i = 1;
            foreach (string item in statsname)
            {
                 //only number type stats are added to the comboboxes.
                if ((statsname[i].ToUpperInvariant()==("TOTALNUMBER")) || ((statsname[i].ToUpperInvariant()==("CURRENTNUMBER"))))
                {
                    comboBox1.Items.Add(statsname[i-1]);
                    i++;
                    i++;
                }
                comboBox1.SelectedIndex = 0;
            }
显然,这并没有正确地检查我需要什么

我需要如何修改我的代码才能得到我所需要的?

Linq来拯救我

var listItems = from s in statsname where s.Equals("TOTALNUMBER", StringComparison.InvariantCultureIgnoreCase) || s.Equals("CURRENTNUMBER", StringComparison.InvariantCultureIgnoreCase) select new ListItem(s);

comboBox1.AddRange(listItems);
代码未经测试或编译,但您可以了解我所说的内容。

Linq来拯救

var listItems = from s in statsname where s.Equals("TOTALNUMBER", StringComparison.InvariantCultureIgnoreCase) || s.Equals("CURRENTNUMBER", StringComparison.InvariantCultureIgnoreCase) select new ListItem(s);

comboBox1.AddRange(listItems);

代码未经测试或编译,但您可以了解我所说的内容。

我不确定您为什么在foreach循环中使用索引。下面的代码应该适合您

foreach (string item in statsname)
        {

            if ( item.ToUpper() == "TOTALNUMBER" || item.ToUpper() == "CURRENTNUMBER")
            {
                comboBox1.Items.Add(item);

            }

        }
        comboBox1.SelectedIndex = 0;

我不知道为什么在foreach循环中使用索引。下面的代码应该适合您

foreach (string item in statsname)
        {

            if ( item.ToUpper() == "TOTALNUMBER" || item.ToUpper() == "CURRENTNUMBER")
            {
                comboBox1.Items.Add(item);

            }

        }
        comboBox1.SelectedIndex = 0;

似乎最好使用for循环而不是foreach:

for (int i = 1; i < statsname.Length; i += 2)
{
    //only number type stats are added to the comboboxes.
    if ((statsname[i].ToUpperInvariant()==("TOTALNUMBER")) || ((statsname[i].ToUpperInvariant()==("CURRENTNUMBER"))))
        comboBox1.Items.Add(statsname[i-1]);
}
for(int i=1;i
似乎最好使用for循环而不是foreach:

for (int i = 1; i < statsname.Length; i += 2)
{
    //only number type stats are added to the comboboxes.
    if ((statsname[i].ToUpperInvariant()==("TOTALNUMBER")) || ((statsname[i].ToUpperInvariant()==("CURRENTNUMBER"))))
        comboBox1.Items.Add(statsname[i-1]);
}
for(int i=1;i
我不需要添加TotalNumber和CurrentNumber。我需要在数组中添加它上面的那个。@AnoushkaseeChour你应该在帖子中这样说。你的帖子建议:“我需要分类为TotalNumber和CurrentNumber的所有项目。”@dcastro,帖子还提到“每个项目在以下索引中都有其分类”。这意味着检查条件是TotalNumber和CurrentNumber,但显示需要是这些项之上的索引项。@anoushkaseecrourn我现在明白了。你不认为一本
字典
更适合这样做吗?有一个数组,其中具有奇数索引的项与具有偶数索引的项具有不同的含义,这会造成混乱。我不需要添加TotalNumber和CurrentNumber。我需要在数组中添加它上面的一个。@AnoushkaseeChour你应该有当时在帖子里这么说的。你的帖子建议:“我需要分类为TotalNumber和CurrentNumber的所有项目。”@dcastro,帖子还提到“每个项目在以下索引中都有其分类”。这意味着检查条件是TotalNumber和CurrentNumber,但显示需要是这些项之上的索引项。@anoushkaseecrourn我现在明白了。你不认为一本
字典
更适合这样做吗?拥有一个数组,其中具有奇数索引的项与具有偶数索引的项具有不同的含义,这会造成混乱。