【问题标题】:adding items into list by for loops通过 for 循环将项目添加到列表中
【发布时间】:2014-02-05 06:41:36
【问题描述】:

我正在为 windows phone 7 创建图表。

这是返回的每个小贩的 foreach:

void dbSvc_retrievepopularhawkerCompleted(object sender, RouteServiceRef.retrievepopularhawkerCompletedEventArgs e)
{
    List<RouteServiceRef.Hawker> recommendPlaceList;
    recommendPlaceList = e.Result.Cast<RouteServiceRef.Hawker>().ToList();

    string hawkername = "";
    string address = "";
    string postal = "";
    double coordX = 0.0;
    double coordY = 0.0;
    double popularity = 0;



    foreach (RouteServiceRef.Hawker rp in recommendPlaceList)
    {
        hawkername = rp.hawkername;
        address = rp.address;
        postal = rp.postal;
        coordX = rp.xcoord;
        coordY = rp.ycoord;
        popularity = rp.popularity;


    }

我必须将上述代码更改为位于此城市列表中:

 List<City> cities = new List<City> { new City { Name = "CLE", Population = 2250871 }, new City { Name = "CMH", Population = 1773120 }, new City { Name = "CVG", Population = 2155137 }, new City { Name = "DET", Population = 4425110 } };

如:List&lt;City&gt; cities = new List&lt;City&gt; { new City Name = hawkername, Population = popularity }

以便我可以绑定到我的图表:

 ColumnSeries bs = ChartControl.Series[0] as ColumnSeries; bs.ItemsSource = cities;

我不知道如何混合它们,有人可以指导我吗?因为我不想硬编码里面的名字和人口。

【问题讨论】:

    标签: c# windows-phone-7 charts silverlight-3.0


    【解决方案1】:

    这很简单,只需按照下面的代码即可。

    在 foreach 循环上方声明 City 列表,并在每次迭代时将新项目添加到列表中

    List<City> cities = new List<City> ();
    
    foreach (RouteServiceRef.Hawker rp in recommendPlaceList)
        {
            hawkername = rp.hawkername;
            address = rp.address;
            postal = rp.postal;
            coordX = rp.xcoord;
            coordY = rp.ycoord;
            popularity = rp.popularity;
            cities.Add(new City(){Name = hawkername, Population = popularity });
        }
    

    这也会在 foreach 循环完成时填充城市列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 2016-02-26
      • 2019-03-02
      • 1970-01-01
      • 2023-03-03
      • 2021-07-01
      • 2020-04-25
      • 1970-01-01
      相关资源
      最近更新 更多