-2

基本的に 3 つの辞書からツリーを構築する方法を理解するのに苦労しています。

こんなふうになります:

  • プロジェクト > シーズン > エピソード

つまり、基本的にはプロジェクトのリストがあり、各プロジェクトには 1 つまたは複数のシーズンがあり、各シーズンには 1 つまたは複数のエピソードがあります。

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 またはラムダ式を使用することもできます

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 に答える