3

您好,我尝试在 C# 中创建一个包含国家、城市和街道的列表。

起初我使用 SQL 从 3 个不同的表中获取数据。然后我想要一个看起来像这样的列表:

   Germany
       Frankfurt
            Siemensweg 14
       Berlin
            Bernstr 4

   USA
        New York
            Atlstr.24
            Zumbastr. 45
   Turkey

这是我到目前为止所拥有的,但它没有按预期工作:

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;

    }
4

6 回答 6

3
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;

        }
于 2013-03-26T10:28:20.753 回答
2

返回 Country 对象列表。Country 对象包含 City 对象的列表。城市对象包含地址对象列表等。

于 2013-03-26T10:09:59.107 回答
2

您需要在这里制作不同的课程。您需要为 Country、City、Street 创建三个类(或更多,取决于您的结构)。国家有一个城市列表,一个城市有一个街道列表。

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; }
}
于 2013-03-26T10:10:55.063 回答
2

您的对象初始化代码不正确。应该:

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;
}
于 2013-03-26T10:10:56.230 回答
1

您需要组织数据。你为什么不使用类来这样做。考虑下面

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;}
}

然后你可以有一个方法

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 

}
于 2013-03-26T10:09:52.117 回答
1

我认为您的结构应如下所示:

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; }

}

获取结构:

Germany
   Frankfurt
        Siemensweg 14
   Berlin
        Bernstr 4

你可以做:

  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 } });
于 2013-03-26T10:11:02.337 回答