2013-03-26 14 views
3

您好,我尝试在C#中创建一个包含国家,城市和街道的列表。如何在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; 

    } 
+1

没有“多维列表”,但是当您看看您在帖子顶部写的内容时,很清楚您需要树形结构。当然,如果树的深度总是达到'街道',你可以用嵌套列表来完成。 – 2013-03-26 10:05:59

+0

目前还不清楚你想要达到的目标。为什么你需要那种列表?要显示一些地址? – 2013-03-26 10:10:26

+0

为什么你需要一个多维列表?你可以简单地使用3个'列表'。一个为国家,一个为城市,另一个为街道。 每个对象包含以下属性: 'int ID,string country/city/street' 然后,您可以简单地使用LINQ来获取任何你想要的。 – jAC 2013-03-26 10:11:31

回答

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; 

     } 
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 

} 
2

返回乡村对象的列表的方法。国家对象包含城市对象的列表。城市对象包含地址对象列表等。

+0

你能举个简单的例子吗? – 2013-03-26 10:10:56

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