Search code examples
c#listloopsdatacontractserializerdatacontract

Iterating through DataContract class


I have a class that returns a Response2 which includes only two definitions I care about:

Response3.Id
Response3.Name

But, this data is returned as a list, b/c my class definition looks like this:

[DataContract]
public class Response2
{
    [DataMember(Name = "done")]
    public bool done;
    [DataMember(Name = "records")]
    public List<Response3> r3entry;
}

[DataContract]
public class Response3
{
    [DataMember(Name = "Id")]
    public string strId { get; set; }
    [DataMember(Name = "Name")]
    public string strName { get; set; }
}

Now I have a List of strings to iterate through, but when I try to do the following:

Response2 propResponse2 = MakeRequest2(propertyRequest2, sfToken);

foreach (string strId in propResponse2)
{
    System.Windows.Forms.MessageBox.Show(strId.Name)
}

I get an error stating:

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

I assume that means I need to add something to the DataContract in the class, but I'm not sure where to do this so I can properly iterate.

Any help?


Solution

  • foreach (var resp3 in propResponse2.r3entry)
    {
        System.Windows.Forms.MessageBox.Show(resp3.strName)
    }