2012-03-19 8 views
1

私はProductのリストを持っています。 。 。ループを使用してオブジェクトのリストの項目を配列に取り込みます

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

。 。 。メソッドと一緒に提供されたtheType引数がListのオブジェクト内のTypeフィールドと一致する場合、Listの項目で配列を設定するメソッドGetList(string theType)を作成したいとします。

返されたときに配列に格納されるのは、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; 
     } 

これは機能していないようです。でも私はそれを見ることができます。私はこれを考えるにはあまりにも疲れている。

EDIT:

私は戻っtheListでコンボボックスを移入します。 2つのコンボボックスがあります。コンボボックス1で選択されたタイプの製品アイテムを取り込むためには、最初のプリセット値を有効にするためにプリセット値を選択しなければなりません。あなたはList<string>ではなく、配列を使用し、一致する項目数がわからないので

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

+0

あなたの編集のために私の答えを編集 –

答えて

6

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

1行のコード、私はそれが好きです。 –

1

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

この代替的に行うことができます私は唯一つのイベントComboBox1のためにハンドリングを持っていますLINQのとはるかに表現力豊かな方法(私の意見で):

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

あなたは現在、あなたのキャプションなどのテキストコンテンツを表示メッセージボックスには、MessageBox.Show 2番目のパラメータであるので、 - あなたの最初の文字がのに改行であるので、あなたは何は表示されません。

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

+0

関連するコードを表示して、実際にあなたのメソッドが一致するアイテムを返すようにしてください。 – BrokenGlass

+0

私はcombobox1のイベント処理のコードで元の投稿を更新しました。 – Bob

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

は、その後、あなたの編集のために

あなたがcombobox.selecteditemを確認することができます選択変更に

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

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

し、リストを取得した後、foreachループを使用する必要があります(コンボボックスを移入します)。テキストLINQで

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(); 
    } 
関連する問題