Harold Finch
Harold Finch

Reputation: 586

Can't execute Foreach for GetEnumerator

When I declare a foreach the compiler show me the following error:

foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator

This is the code:

leagueTable_link = item.Links.LeagueTable.href.ToString();
object leagueTable_object = load_League_Table(leagueTable_link);

foreach (var classifica in leagueTable_object)
{

}

And this is the class created by the online tool Json2C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
class LeagueTable
{
    public string[] _links = new string[50]; 
    public int[] position = new int[50];
    public string[] teamName = new string[50];
    public int[] playedGames = new int[50];
    public int[] points = new int[50];
    public int[] goals = new int[50];
    public int[] goalsAgainst = new int[50];
    public int[] goalDifference = new int[50];
    public struct League_Struct
    {
        public string _links;
        public int position;
        public string teamName;
        public int playedGames;
        public int points;
        public int goals;
        public int goalsAgainst;
        public int goalDifference;
    }
    public League_Struct[] league_struct = new League_Struct[50];
    public class Links
    {
        public string self { get; set; }
        public string soccerseason { get; set; }
    }
    public class Team
    {
        public string href { get; set; }
    }
    public class Links2
    {
        public Team team { get; set; }
    }
    public class Standing
    {
        public Links2 _links { get; set; }
        public int position { get; set; }
        public string teamName { get; set; }
        public int playedGames { get; set; }
        public int points { get; set; }
        public int goals { get; set; }
        public int goalsAgainst { get; set; }
        public int goalDifference { get; set; }
    }
    public class RootObject
    {
        public  Links _links { get; set; }
        public string leagueCaption { get; set; }
        public int matchday { get; set; }
        public List<Standing> standing { get; set; }
    }
}

}

The leagueTable_object contain this:

var obj = JsonConvert.DeserializeObject<LeagueTable.RootObject>(responseText);

How can I fix this? I see online different solution but I don't understood. Should I implement GetEnumerator class in my Json class?

Upvotes: 0

Views: 1032

Answers (3)

PMerlet
PMerlet

Reputation: 2594

The type of your leagueTable_object object seems to be 'System.Data.DataTable' which is not an enumerable type so you can't loop on this object. What you can do is loop on the rows in this DataTable :

leagueTable_link = item.Links.LeagueTable.href.ToString();
DataTable leagueTable_object = load_League_Table(leagueTable_link);

foreach (var classifica in leagueTable_object.Rows)
{

}

Upvotes: 1

Dhrumil
Dhrumil

Reputation: 3204

If your object here is a DataTable type, then the problem here is that you are trying to iterate on a DataTable object whereas you need to iterate on its rows. So try changing your foreach code to something like this.

 foreach (var classifica in leagueTable_object.Rows)
                        {

                        }

You can also cast leagueTable_object directly as DataTable instead of using object. That would make things more clear to the viewer since he would know which type of object is created.

Hope this helps.

Upvotes: 0

Spider man
Spider man

Reputation: 3330

It looks like leagueTable_object is an object of DataTable.If it is a list then it will work. Foreach you can use for classes which are inheriting IEnumerable.You can use leagueTable_object.Rows instead of leagueTable_object in foreach.

Upvotes: 0

Related Questions