here is i am trying to work.
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 here:
Error 2 foreach statement cannot operate on variables of type Registration because Registration does not contain a public definition for 'GetEnumerator'
i have a class called MasterEmployee and in it i have a with few props and few methods on it
[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;
}
}
}
The explanation provided in the error message is clear enough. You are trying to iterate
item.Registration, which is an instance ofRegistration. However,Registrationis not derived from an iterable type, and does not implement theGetEnumeratorfunction required for custom iterable types. So it cannot be iterated using aforeachloop.But I believe either your naming conventions are incorrect, or you have misunderstood your data model. Why would a
Registrationinstance ever contain a collection ofRegistrationinstances? If an item can have multipleRegistrationinstances associated with it, then the property should be called something likeitem.Registrations, and it should not be of typeRegistration, it should be a list/collection type that containsRegistrationinstances.