0

I'm serializing a multi-level json object. These are called webinars and each contain information like below. I can access the first level with no problems. I can access the 2nd level (categories >) if I do so like webinars[webinar].categories.TimeZone etc but I can't loop through it. When I try to foreach(category in webinar) {} I get:

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

What have I done wrong?

thanks for your help.

SAMPLE JSON

{
  "date": "2017-11-08T02:33:57Z",
  "title": "Craft, The",
  "desc": "Galeazzi's fx l rad, subs for opn fx type 3A/B/C w malunion",
  "startTime": "4:03 AM",
  "endTime": "5:46 PM",
  "categories": {
    "category": "e-learning",
    "timeZone": "Asia/Jakarta",
    "language": "English"
  }
}

Model

public class Rootobject
{
    public Webinar[] Webinars { get; set; }
    public ELearning[] Elearning { get; set; }
}

public class Webinar
{
    public DateTime Date { get; set; }
    public string Title { get; set; }
    public string Desc { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public WebinarCategories Categories { get; set; }
}

public class WebinarCategories
{
    public string Category { get; set; }
    public string TimeZone { get; set; }
    public string Language { get; set; }
}

What I'm trying to achieve

foreach (var webinar in root.webinars) {
     foreach (var category in webinar) {

            <button href="javascript:void(0)" class="dropbtn">@category</button>
     }
}
2
  • 2
    The WebinarCategories Class isn't either an Array or a List. Commented Aug 25, 2017 at 11:56
  • it doesn't work since your Categories is not type of IENumerable Commented Aug 25, 2017 at 11:56

2 Answers 2

3

Is pretty simple why this won't work.

If you want to loop through the categories you have to write

foreach (var category in webinar.Categories)

Then will you get an error because WebinarCategories does not implement GetEnumerator because it does not implement IEnumerable. If you want to store a list of categories in your webinar the easiest way would be to change your property like this.

public List<WebinarCategories> Categories { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That was exactly my problem.
0

Change public WebinarCategories Categories { get; set; }

to public List<WebinarCategories> Categories { get; set; }

or, if you really want to IEnumerable<WebinarCategories> Categories { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.