Getting NotImplementedException on foreach

197 Views Asked by At

Trying to get data from service and bind it to grid

I'm getting the data in localitem and the error occurs on the "foreach" line

any help would be appreciated

private IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> Getlocalinfo(string personalNO)
{
    needsUpdate = false;

    using (PersonalInfoServiceClient serviceclient = new PersonalInfoServiceClient())
    {
        List<PersonalIDCheckerMvCKendo.Models.PersonInfo> personInfo = new List<PersonalIDCheckerMvCKendo.Models.PersonInfo>();

        try
        {
            IEnumerable localItem = serviceclient.GetLocalInfoForPerson(personalNO);

            if (localItem != null)
            {
                foreach (PersonalIDCheckerMvCKendo.Models.PersonInfo dalitem in localItem)
                {
                    personInfo.Add(new PersonalIDCheckerMvCKendo.Models.PersonInfo
                    {
                        DocumentSerie = dalitem.DocumentSerie,
                        DocumentNumber = dalitem.DocumentNumber,
                        DocumentType = dalitem.DocumentType,
                        DocumentIssuer = dalitem.DocumentIssuer,
                        DocumentValidDate = dalitem.DocumentValidDate
                    });                        
                }
            }
            return personInfo.ToArray();
        }
        catch
        {
            throw;
        }
        finally
        {
            serviceclient.Close();
        }
    }
}

EDIT:

GetLocalInfoForPerson is of type PersonalInformation

public partial class PersonalInformation : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged , System.Collections.IEnumerable 
1

There are 1 best solutions below

4
On

What concrete type does GetLocalInfoForPerson return ? Does it implement IEnumerable properly ?

Based on your code sample, it is hard to say what might be wrong since we don't know the types involved. However, you type the localItem as IEnumerable, and if this is a custom implementation, someone might have forgotten to implement the non-generic GetEnumerator method.

Do you get an error if you change the declaration of localItem to be:

IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> localItem = serviceclient.GetLocalInfoForPerson(personalNO);

?

This would imply that IEnumerable.GetEnumerator is not implemented (ie. throws NotImplementedException), while IEnumerable<T>.GetEnumerator is in fact implemented. It could also be that both methods are not implemented. Look it up in the code for the type that is returned from GetLocalInfoForPerson.