-2

我很难弄清楚如何从基本上三个字典中构建一棵树。

它是这样的:

  • 项目 > 季节 > 剧集

所以基本上我有一个项目列表,然后每个项目可以有一个或多个季节,然后每个季节可以有一个或多个剧集。

我将如何为此创建一个树结构,以便我可以将其作为 JSON 发送回来?

我非常感谢任何帮助,因为我正在努力解决这个问题。

这是我开始的:

//projects
        var projectsDictionary = new Dictionary<int, string>();
        var ProjectSearchResult = new ProjectSearchResultController();
        var ProjectSearchCriteria = new GBLProjectSearchCriteria
        {
                ProjectName = projectName, SearchType = "P", QueryString = "?ProjectId="
        };
        var GBLProjectSearchResultListData = ProjectSearchResult.GetProjectSearchResultList(ProjectSearchCriteria);
        foreach (GBLProjectSearchResult item in GBLProjectSearchResultListData)
        {
            projectsDictionary.Add(item.Id, item.Title);
        }
        foreach (var project in projectsDictionary)
        {
            //seasons
            var seasonsDictionary = new Dictionary<int, string>();
            var SeasonSearchResult = new ProjectSearchResultController();
            var SeasonSearchCriteria = new GBLProjectSearchCriteria
            {
                    Id = project.Key, ProjectName = projectName, SearchType = "S", QueryString = "?ProjectId=" + projectId + "&SeasonId=",
            };
            var GBLSeasonSearchResultListData = SeasonSearchResult.GetProjectSearchResultList(SeasonSearchCriteria);
            foreach (GBLProjectSearchResult item in GBLSeasonSearchResultListData)
            {
                seasonsDictionary.Add(item.Id, item.Title);
            }
            foreach (var season in seasonsDictionary)
            {
                //episodes
                var episodesDictionary = new Dictionary<int, string>();
                var episodeSearchResult = new ProjectSearchResultController();
                var episodeSearchCriteria = new GBLProjectSearchCriteria
                {
                        Id = season.Key, ProjectName = projectName, SearchType = "E", QueryString = "?ProjectId=" + projectId + "&SeasonId=" + seasonId + "&EpisodeId=",
                };
                var GBLEpisodeSearchResultListData = episodeSearchResult.GetProjectSearchResultList(episodeSearchCriteria);
                foreach (GBLProjectSearchResult item in GBLEpisodeSearchResultListData)
                {
                    episodesDictionary.Add(item.Id, item.Title);
                }
            }
        }
4

1 回答 1

2

当前的方法不起作用可能是这将帮助您创建三个名为的类

项目季节剧集

public class Episodes
{
    public int Id { get; set; }
    public string Title { get; set; }
}

public class Seasons
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IList<Episodes> { get; set; }
}

public class Project
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IList<Seasons> { get; set; }
}

只需在您的方法顶部创建一个新的项目列表

IList<Project> projectList = new List<Project>();

之后在填充数据时这样做

IList<Project> projectList = new List<Project>();
var ProjectSearchResult = new ProjectSearchResultController();
var ProjectSearchCriteria = new GBLProjectSearchCriteria
{
        ProjectName = projectName, SearchType = "P", QueryString = "?ProjectId="
};
var GBLProjectSearchResultListData = ProjectSearchResult.GetProjectSearchResultList(ProjectSearchCriteria);
foreach (GBLProjectSearchResult item in GBLProjectSearchResultListData)
{
    Project project = new Project();
    project.Id = item.Id;
    project.Title = item.Title;
    projectList.Add(project);
}
foreach (var project in projectList)
{
    //seasons
    project.Seasons = new List<Seasons>();
    var SeasonSearchResult = new ProjectSearchResultController();
    var SeasonSearchCriteria = new GBLProjectSearchCriteria
    {
            Id = project.Key, ProjectName = projectName, SearchType = "S", QueryString = "?ProjectId=" + projectId + "&SeasonId=",
    };
    var GBLSeasonSearchResultListData = SeasonSearchResult.GetProjectSearchResultList(SeasonSearchCriteria);
    foreach (GBLProjectSearchResult item in GBLSeasonSearchResultListData)
    {
        Seasons season = new Seasons();
        season.Id = item.Id;
        season.Title = item.Title;
        project.Seasons.Add(season);
    }
    foreach (var season in project.Seasons)
    {
        //episodes
        season.Episodes = new List<Episodes>();
        var episodeSearchResult = new ProjectSearchResultController();
        var episodeSearchCriteria = new GBLProjectSearchCriteria
        {
                Id = season.Key, ProjectName = projectName, SearchType = "E", QueryString = "?ProjectId=" + projectId + "&SeasonId=" + seasonId + "&EpisodeId=",
        };
        var GBLEpisodeSearchResultListData = episodeSearchResult.GetProjectSearchResultList(episodeSearchCriteria);
        foreach (GBLProjectSearchResult item in GBLEpisodeSearchResultListData)
        {
            Episodes episode = new Episodes();
            episode.Id = item.Id;
            episode.Title = item.Title;
            season.Episodes.Add(episode);
        }
    }
}

或者,您也可以使用 LINQ 或 Lambda 表达式,而不是编写这么多代码

用于 JSON 格式的数据序列化

您可以创建扩展方法:

    public static string ToJson<T>(this T objectToBeConverted, Func<T, object> filter)
    {
        return new JavaScriptSerializer().Serialize(filter(objectToBeConverted));
    }

过滤器可以这样创建:

Func<IList<Project>, object> filter =
                new Func<IList<Project>, object>(projects => projects.Select(project => new { project.Id, project.Title }));
于 2013-08-09T17:28:50.247 回答