Eray Geveci
Eray Geveci

Reputation: 1129

How to generate a multidimensional List<> in C#

Hello i try to create a list in C# which contains countries,city and street.

At first i use SQL to fetch from the 3 different Tables the data. then i want a list which look like this :

   Germany
       Frankfurt
            Siemensweg 14
       Berlin
            Bernstr 4

   USA
        New York
            Atlstr.24
            Zumbastr. 45
   Turkey

this is what i have so far, but its not working as expected:

public class iOrt
{
    public class ORT
    {
        public string RegionID { get; set; }
        public string RegionName { get; set; }
        public List<STADT> Stadt { get; set; }

    }

    public class STADT
    {
      public string Stadname { get; set; }
    }


    //SQL Verbindung wird ausgelesen
    protected static string GetConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["Bookit"].ConnectionString;
    }

    internal static List<ORT> Ort()
    {
        List<ORT> ortObject = new List<ORT>();
        List<STADT> stadtObject = new List<STADT>();

        ortObject.Add(new ORT {
        RegionID="235",
        RegionName="deutschland",
        Stadt = stadtObject.Add(new STADT{

            Stadname="Frankfurt"})


        });

        return ortObject;

    }

Upvotes: 3

Views: 13765

Answers (6)

Nasir Mahmood
Nasir Mahmood

Reputation: 1505

public class Street
    {
        public string Name { get; set; }
    }

    public class City
    {
        public string Name { get; set; }
        public List<Street> Streets { get; set; }
    }

    public class Country
    {
        public string Name { get; set; }
        public List<City> Cities { get; set; }
    }

    public static List<Country> Ort()
        {
            List<Country> countries = new List<Country>();
            countries.Add(new Country()
            {
                Name = "Country1",
                Cities = new List<City>()
                {
                    new City()
                    {
                        Name="City1",
                        Streets=new List<Street>()
                        {
                            new Street()
                            {
                                Name="Street 1"
                            },
                            new Street()
                            {
                                Name="Street 2"
                            }
                        }
                    },
                    new City()
                    {
                        Name="City2",
                        Streets=new List<Street>()
                        {
                            new Street()
                            {
                                Name="Street 1"
                            },
                            new Street()
                            {
                                Name="Street 2"
                            }
                        }
                    }
                }
            });
            return countries;

        }

Upvotes: 3

Manish Mishra
Manish Mishra

Reputation: 12375

You need to organize your data. Why don't you use classes to do so. consider below

public class County
{
    public string Name{get;set;}
    public List<City> Cities{get;set;}
}

public class City
{
    public string Name{get;set;}
    public List<Region> {get;set;}
}

public class Region
{
   public string Name{get;set;}
   public int Code{get;set;}
}

and then you can have a method like

internal static List<Country> Ort()
{
   Country country = new Country();
   Country.Name="Germany";
   Country.Cities = new List<City>();  

   City city1 = new City();
   city1.Name="Frankfurt";
   city1.Regions = new List<Region>();

   Region region = new Region(); 
   region.Name = "Siemensweg";
   region.code = 14;

   city1.Regions.Add(region);

   region = new Region();
   region.Name = Bernstr;
   region.Code = 4;

   city1.Regions.Add(region);

   country.Cities.Add(city1);

   List<Country> countries = new List<Country>();
   countries.Add(country);

   return countries;   

   //or you can make your classes in a loop on a table row collection 

}

Upvotes: 1

Darren
Darren

Reputation: 70718

I think your structure should look like:

public class Country {
      public string CountryName { get; set; }
      public List<CountryRegion> Regions { get; set; } 
}

pulic class CountryRegion 
{
     public string RegionName { get; set; }  
     public List<Area> Areas { get; set; }    
}

public class Area {
     public string AreaName { get; set; }
     public int Population { get; set; }

}

To get the structure:

Germany
   Frankfurt
        Siemensweg 14
   Berlin
        Bernstr 4

You can do:

  var Germany = new Country();
  Germany.CountryName = "Germany";
  Germany.Regions = new List<CountryRegion>();

  var Siemensweg = new Area();
  Siemensweg.AreaName = "Siemensweg";
  Siemensweg.Population = 14;

  var Bernstr = new Area();
  Bernstr.AreaName = "Bernstr";
  Bernstr.Population = 4;

  Germany.Regions.Add(new CountryRegion { RegionName = "Siemensweg", new List<Area> { Siemensweg } });
  Germany.Regions.Add(new CountryRegion { RegionName = "Berlin", new List<Area> { Bernstr } });

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Your object initialization code is incorrect. Should be:

internal static List<ORT> Ort()
{
    List<ORT> ortObject = new List<ORT>();

    ortObject.Add(new ORT
                  {
                      RegionID="235",
                      RegionName="deutschland",
                      Stadt = new List<STADT>()
                      {
                          new STADT { Stadname="Frankfurt" }
                      }
                  });

    return ortObject;
}

Upvotes: 2

fhnaseer
fhnaseer

Reputation: 7267

You need to make different classes here. You need to make three classes (or more, depend on your structure) for Country, City, Street. Country has a list of Cities, and a city has a list of streets.

public class Country
{
    public string Name { get; set; }
    public List<City> Cities { get; set; }
}

public class City
{
    public string Name { get; set; }
    public List<Street> Streets { get; set; }
}

public class Street
{
    public string Name { get; set; }
}

Upvotes: 2

Jack Hughes
Jack Hughes

Reputation: 5654

Return a list of Country objects. Country objects contain a list of City objects. City objects contain a list of Address objects etc.

Upvotes: 2

Related Questions