2011-08-10 8 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 인스턴스의 컬렉션이 포함 된 이유는 무엇입니까? 하나의 항목에 여러 개의인스턴스가 연결되어있을 수있는 경우이 속성은 item.Registrations과 같은 형식이어야하며 형식이 Registration이 아니어야합니다. 에 이 포함 된 목록/컬렉션 형식이어야합니다.Registration 인스턴스를 포함해야합니다.

+0

그래서 문제의 해결책은 무엇입니까? –

+0

@Abu - 먼저 무엇을하려고하는지 명확히 한 다음 거기에서부터 시작하십시오. 앞에서 언급했듯이, 실제로는'Registration' 인스턴스가'Registration' 인스턴스의 목록을 포함하는 반복 가능한 클래스가되도록하고 싶지 않습니다. 'ArrayList'와 같은 것을 사용하고 거기에'Registration' 인스턴스를 넣기를 원할 것입니다. 그런 다음 '등록'이 아닌 * 목록 *을 반복합니다. – aroth

+0

당신은 데이터 모델 클래스의 라이트를 보았습니까? 그래서 iam tyring은리스트를 반복하는 것입니다. 등록의 ... thats consits of Registration ... Students .... so forth ... –

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

죄송합니다. 늦었습니다. 피곤해집니다.) class MasterEmployee가 문제가되지 않습니다. 클래스 등록은 다음과 같습니다. Collectionclass가 아닙니다. 나는 당신이 두 번째 foreach를하는 이유를 알지 못한다. 그것은 단지 하나의 가치이다. 클래스 등록은 코드 니펫에 없습니다. –