将列表添加到列表并从中读取?

时间:2014-05-07 11:53:42

标签: c# list windows-phone-8 nested-lists

我有一个问题,我需要将List存储在另一个类/列表中。

我有这堂课:

public class InformationMyTravels
{
    public string MyTravelsDate { get; set; }
    public string MyTravelsFromLocation { get; set; }
    public string MyTravelsToLocation { get; set; }
}

我可以填充List,然后将其保存到我的隔离存储(AppSettings)。 问题是,当我在下面有多个“概述”时,以下代码将只附加旅行历史记录。 我需要的是“概述”的分离,以便为每个概述填充的列表I保存在另一个类/列表中,该列表/列表可以包含我获取的x-number“概览”列表。

private async Task Fetch()
{
    AppSettings localStorage = new AppSettings();

    List<InformationMyTravels> mytravelsreturned = new List<InformationMyTravels>();

    // I need to separate the returned data per Overview
    foreach (Overview loaded in localStorage.OverviewSetting)
    {                    
        string mytravelsHtml = await WebRequests.LoadPageAsyncSpecificRKMyTravels(loaded.CardOverviewID);

        HtmlDocument htmlDocumentmytravels = new HtmlDocument();
        htmlDocumentmytravels.LoadHtml(mytravelsHtml);
        foreach (HtmlNode table in htmlDocumentmytravels.DocumentNode.SelectNodes("//table[@class='table']"))
        {
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {
                InformationMyTravels newTravel = new InformationMyTravels();
                newTravel.MyTravelsDate = row.SelectSingleNode("td[1]").InnerText.Trim();
                newTravel.MyTravelsFromLocation = row.SelectSingleNode("td[3]").InnerText.Trim();
                newTravel.MyTravelsToLocation = row.SelectSingleNode("td[5]").InnerText.Trim();
                // Here it just appends with newTravel's
                mytravelsreturned.Add(newTravel);
            }
        }
        mytravelsreturned.Reverse();
    }
    localStorage.MyTravelsSetting = mytravelsreturned;
}

那么如何将“mytravelsreturned”添加到另一个Class / List中呢? 然后我需要从新类中选择特定的listindex并将旅行加载到listbox.ItemsSource

通缉层级:

  • 类/列表
    • InformationMyTravels(0)
      • MyTravelsDate(0)
      • MyTravelsFromLocation(0)
      • MyTravelsToLocation(0)
      • MyTravelsDate(1)
      • MyTravelsFromLocation(1)
      • MyTravelsToLocation(1)
    • InformationMyTravels(1)
      • MyTravelsDate(0)
      • MyTravelsFromLocation(0)
      • MyTravelsToLocation(0)
      • MyTravelsDate(1)
      • MyTravelsFromLocation(1)
      • MyTravelsToLocation(1)

然后我需要将例如InformationMyTravels(1)加载到listbox.ItemsSource

我希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

以下是未经测试的代码(我目前不在Windows上)。它应该给你的想法。

Class InformationMyTravelsList<T> : List<T>
{
    public int id{get; private set;}

    public void AddInfo(int ID, InformationMyTravels info)
    {
        this.id = ID;
        this.Add(info);
    }

}

Class Main
{
    void myLogic()
    {
        InformationMyTravels info = new InformationMyTravels();

        InformationMyTravelsList<InformationMyTravels> infoList = new InformationMyTravelsList<InformationMyTravels>();
        infoList.AddInfo(info); //add as many you want

        List<InformationMyTravelsList<InformationMyTravels>> myList = new InformationMyTravelsList<InformationMyTravels>>();
        myList.Add(infoList);

        myListBox.DataSource = myList;

    }
}