2011-08-10 9 views
0

の変数に対しては動作できません。ここでは、私は働こうとしています。ここforeach文は、タイプ

List<MasterEmployee > masterEmployee = new List<MasterEmployee >(); 
masterEmployee = MasterEmployee.GetAll("123"); //connecting db and returning a list... 


    foreach (MasterEmployee item in masterEmployee) 
    { 
     foreach (Registration reg in item.Registration) //<<<error here... 
     { 
      // 
     } 
    } 

エラー:

Error 2 foreach statement cannot operate on variables of type Registration because Registration does not contain a public definition for 'GetEnumerator' 

私はMasterEmployeeというクラスを持っており、それには、私はいくつかの小道具や、それにはいくつかの方法

[Serializable] 
    public class MasterEmployee 
    { 

     //few props omitted .... 

     protected Registration _registration; 
     [CopyConstructorIgnore] 
     public Registration Registration 
     { 
      get 
      { 
       return _registration; 

      } 
      set 
      { 
       this._registration = value; 
      } 
     } 
     protected User _user; 
     [CopyConstructorIgnore] 
     public User MyUser 
     { 
      get 
      { 
       return _user; 
      } 
      set 
      { 
       this._user= value; 
      } 
     } 

     protected Student _student; 
     [CopyConstructorIgnore] 
     public Student Student 
     { 
      get 
      { 
       return _student; 
      } 
      set 
      { 
       this._student = value; 
      } 
     } 
} 

答えて

3

説明は十分に明らかです。 item.Registrationを反復しようとしています。これはRegistrationのインスタンスです。ただし、Registrationは反復可能型から派生したものではなく、カスタム反復可能型に必要な関数GetEnumeratorを実装していません。したがって、foreachループを使用して反復することはできません。

しかし、私はあなたの命名規則が間違っているか、あなたのデータモデルを誤解していると思います。 RegistrationインスタンスにRegistrationインスタンスのコレクションが含まれるのはなぜですか? 1つのアイテムに複数のインスタンスが関連付けられている場合、そのプロパティはitem.Registrationsのように呼び出され、タイプがRegistrationである必要はありません。にはリスト/コレクションタイプである必要があります。Registrationインスタンスです。

+0

だから問題の解決方法は? –

+0

@Abu - まずあなたがしようとしていることを明確にし、そこから行ってください。私が指摘したように、実際には 'Registration'を' Registration'インスタンスのリストを含む反復可能なクラスにしたいとは思っていません。おそらく 'ArrayList'のようなものを使い、その内部に' Registration'インスタンスを置くことができます。次に、 'Registration'ではなく* list *を反復処理します。 – aroth

+0

あなたはデータモデルクラスの儀式を見たことがありますので、達成するために何をすべきかは、リストを反復する方法です...登録の... thatsのconss ...学生....など... –

1

でクラスから派生する必要がありますしていますIEnumerable。

基準と例:エラーメッセージで提供さhttp://msdn.microsoft.com/de-de/library/system.collections.ienumerable%28VS.80%29.aspx

+0

IEnumerableを実装する方法は他にありませんか? –

+0

これは大きな問題ではありません。あなたのクラスの宣言を "public class MasterEmployee:IEnumarable"に変更し、関数を追加しますpublic IEnumerator GetEnumerator(){} –

+0

残念ですが、私は疲れています;)クラスMasterEmployeeは問題ではありません。 Collectionクラスではありません。私はなぜあなたが二番目のforeachを行うのか、ポイントを見ていない、それは単なる値です。しかし、クラス登録はコードニットにはありません。 –