2012-03-19 93 views
1

我有一个类型为Product的列表。 。 。使用循环从对象列表中填充项目数组

public List<Product> products = new List<Product>(); 

。 。 。并且我想创建一个方法GetList(string theType),如果该方法提供的theType参数与List中的任何对象内的Type字段匹配,该方法将使用该List中的项目填充数组。

只有我想返回时,数组包含的东西是所有那些反对提供theType参数匹配成功的产品

public string[] GetList(string theType) 
     { 
      string[] theList = new string[10]; 
      for(int i = 0; i < theList.Length; i++) 
      { 
       foreach (Product p in products) 
       { 
        if (p.Type.Equals(theType)) 
        { 
         theList[i] = p.ProductName; 
        } 
       } 
      } 
      return theList; 
     } 

这似乎并不奏效。即使我可以看到它。我太累了想不出来。

编辑:

我想填充ComboBox与返回theList。 有两个组合框。您必须在第一个选项中选择一个预设值以启用第二个预设值,并且应该使用combobox1中选择的产品类型填充第二个预设值。我只有一个事件处理对于ComboBox:

private void combobox1_SelectedValueChanged(object sender, EventArgs e) 
      { 
       if (combobox1.Text != "") 
       { 
        combobox2.Enabled = true; 
        combobox2.Items.Clear(); 

        if (combobox1.SelectedText.Equals("Dairy")) 
        { 
// i try to display what the method has returned inside a messagebox, but it doesn't display it at all, the messagebox 
         string[] theList = client.GetList("dairy"); 
         string theStringList = ""; 

         for (int i = 0; i < theList.Length; i++) 
         { 
          theStringList += "\n" + theList[i]; 
         } 
         MessageBox.Show(String.Format("{0}"), theStringList); 
         //combobox2.Items.AddRange(client.GetList("dairy")); 
        } 
       } 
       else 
        combobox2.Enabled = false; 
      } 
+0

使用==不等于。 – Joe 2012-03-19 01:10:03

+0

编辑我的答案,以适应您的编辑 – 2012-03-19 11:37:43

回答

6

使用LINQ:

return products.Where(p => p.Type == theType).Select(p => p.ProductName).ToArray(); 
+0

1行代码,我喜欢。 – 2012-03-19 11:25:58

1

既然你不知道有多少项目会匹配,使用List<string>,而不是一个数组:

public IList<string> GetList(string theType) 
{ 
    List<string> matchingProductNames = new List<string>(); 
    foreach (Product p in products) 
    { 
     if (p.Type == theType) 
      matchingProductNames.Add(p.ProductName); 
    } 
    return matchingProductNames; 
} 

这或者也可以在做一个更富于表现力的LINQ的方式(在我看来):

string[] productNames = products.Where(p => p.Type == theType) 
           .Select(p => p.ProductName) 
           .ToArray(); 

您目前显示文本内容的标题您消息框,因为它是MessageBox.Show第二个参数 - 既然你第一个字符是一个换行符虽然你不会看到任何东西

MessageBox.Show(String.Format("{0}"), theStringList); 

你可能意味着MessageBox.Show(string.Format("{0}", theStringList))但没有一点使用string.Format这是因为你没有应用格式化,所以首先直接使用字符串:

string theStringList = string.Join("\n", client.GetList("dairy")); 
MessageBox.Show(theStringList, "some caption"); 
+0

我尝试了在这里建议的一切。问题是我想使用返回的'theList'来使用'AddRange()'填充组合框,但它不会更新。 – Bob 2012-03-19 01:21:39

+0

你应该显示相关的代码然后 - 还要确保你的方法实际上确实返回匹配项 – BrokenGlass 2012-03-19 01:24:18

+0

我已经更新了原始帖子和事件处理的combobox1代码。 – Bob 2012-03-19 01:31:31

1

你为什么要把它放在for循环中?

public List<String> GetList(string theType) 
    { 
     List<String> TheList = new List<String>(); 

      foreach (Product p in products) 
      { 
       if (p.Type = theType)) 
       { 
        theList.add(ProductName); 
       } 
      } 

     return theList; 
    } 

然后为您编辑(填充组合框)

你应该得到的名单

List<String> iList = GetList("typenamehere") 

    foreach(string s in theList){ 
    Combobox1.Add(s); 
    } 

然后选择更改,您可以检查combobox.selecteditem后使用foreach循环。文字

1
public string[] GetList(String theType) 
    { 
     ArrayList theList = new ArrayList(); 
     foreach (Product p in products) 
     { 
      if (p.GetType().ToString() == theType) 
       theList.Add(p.ProductName); 
     } 
     return theList.Cast<string>().ToArray(); 
    }