Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何转换为通用代码?_C#_Treeview - Fatal编程技术网

C# 如何转换为通用代码?

C# 如何转换为通用代码?,c#,treeview,C#,Treeview,我正在尝试制作一个树状视图,其中包含以下顺序的项目 + Continent + Country + Province + Territory 我有一个泛型类来保存treeView项数据 public class TreeViewContinents { public List<TreeViewContinents> Children { get; set; } public TreeViewContinents

我正在尝试制作一个树状视图,其中包含以下顺序的项目

+ Continent
    + Country
        + Province
            + Territory
我有一个泛型类来保存treeView项数据

public class TreeViewContinents
{
    public List<TreeViewContinents> Children { get; set; }    
    public TreeViewContinents Parent { get; set; }    
    public string Name { get; set; }    
    public string Content { get; set; }
}
代码:

private readonly List<Dictionary<string, object>> continentsList = new List<Dictionary<string, object>>();

/*
continentsList[0] - "Continent", "Asia"
                    "Country", "Afghanistan"
                    "Province", "Kabul"
                    "Territory", "Bagrami"
                    "Language", "aaa"
                    "Culture", "bbb"
                .
                .               
                .
                .

continentsList[n] - "Continent", "North America"
                    "Country", "Canada"
                    "Province", "Ontario"
                    "Territory", "Ottawa"
                    "Language", "aaa"
                    "Culture", "bbb

*/
List<object> Continents = this.continentsList.Where(
                            p_oDict => p_oDict.ContainsKey(treeViewItemNames[0]))
                            .Select( p_oDict => p_oDict[treeViewItemNames[0]])
                            .Distinct()
                            .ToList();

var topItems = new List<TreeViewContinents>();

foreach (object continent in Continents)
{
    var level1Items = new TreeViewContinents { Name = treeViewItemNames[0], Content = continent.ToString() };

    List<object> Countries = this.continentsList.Where(
                            p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                            && p_oDict.ContainsKey(treeViewItemNames[1])
                            && p_oDict[treeViewItemNames[0]].Equals(continent)
                            .Select( p_oDict => p_oDict[treeViewItemNames[1]])
                            .Distinct()
                            .ToList();

    Countries.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
    foreach (object country in Countries)
    {
        var level2Items = new TreeViewContinents { Name = treeViewItemNames[1], Content = country.ToString(), Parent = level1Items };
        level1Items.Children.Add(level2Items);

        List<object> Provinces = this.continentsList.Where(
                               p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                                        && p_oDict.ContainsKey(treeViewItemNames[1])
                                          && p_oDict.ContainsKey(treeViewItemNames[2])
                                          && p_oDict[treeViewItemNames[0]].Equals(continent)
                                          && p_oDict[treeViewItemNames[1]].Equals(country))
                                          .Select( p_oDict => p_oDict[treeViewItemNames[2]]).Distinct()
                                        .ToList();

        Provinces.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
        foreach (object province in Provinces)
        {
            var level3Items = new TreeViewContinents { Name = treeViewItemNames[2], Content = province.ToString(), Parent = level2Items };
            level2Items.Children.Add(level3Items);

            List<object> Territories = this.continentsList.Where(
                               p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                                        && p_oDict.ContainsKey(treeViewItemNames[1])
                                        && p_oDict.ContainsKey(treeViewItemNames[2])
                                        && p_oDict.ContainsKey(treeViewItemNames[3])
                                        && p_oDict[treeViewItemNames[0]].Equals(continent)
                                        && p_oDict[treeViewItemNames[1]].Equals(country)
                                        && p_oDict[treeViewItemNames[2]].Equals(province)).Select(
                                        p_oDict => p_oDict[treeViewItemNames[3]])
                                        .Distinct()
                                        .ToList();
            Territories.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
            foreach (object territory in Territories)
            {
                var level4Items = new TreeViewContinents { Name = treeViewItemNames[3], Content = territory.ToString(), Parent = level3Items };
                level3Items.Children.Add(level4Items);
            }
        }
    }

    topItems.Add(level1Items);
}
它应该像这样生成treeView

+ Continent
    + Country
        + Territory

请提供想法/建议。

我建议您浏览大陆列表元素

    var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };

     var topItems = new List<TreeViewContinents>();


     foreach (var continent in continentsList)
     {
         List<TreeViewContinents> currentLevel = topItems;
         TreeViewContinents parentItem = null;
         foreach (var sectionTitle in treeViewItemNames)
         {
             String value = Convert.ToString(continent[sectionTitle]);
             TreeViewContinents currentItem = currentLevel.FirstOrDefault(tree => tree.Content == value);
             if (currentItem == null)
             {
                 currentItem = new TreeViewContinents { Name = sectionTitle, Content = value };
                 currentItem.Children = new List<TreeViewContinents>();
                 if (parentItem != null)
                 {
                     currentItem.Parent = parentItem;
                 }
                 currentLevel.Add(currentItem);
             }
             parentItem = currentItem;
             currentLevel = currentItem.Children;
         }
     }

是,orderBy参数打印错误。返回treeViewList.OrderBy(it=>it.Content.ToList();固定的
+ Continent
    + Country
        + Territory
    var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };

     var topItems = new List<TreeViewContinents>();


     foreach (var continent in continentsList)
     {
         List<TreeViewContinents> currentLevel = topItems;
         TreeViewContinents parentItem = null;
         foreach (var sectionTitle in treeViewItemNames)
         {
             String value = Convert.ToString(continent[sectionTitle]);
             TreeViewContinents currentItem = currentLevel.FirstOrDefault(tree => tree.Content == value);
             if (currentItem == null)
             {
                 currentItem = new TreeViewContinents { Name = sectionTitle, Content = value };
                 currentItem.Children = new List<TreeViewContinents>();
                 if (parentItem != null)
                 {
                     currentItem.Parent = parentItem;
                 }
                 currentLevel.Add(currentItem);
             }
             parentItem = currentItem;
             currentLevel = currentItem.Children;
         }
     }
public List<TreeViewContinents> SortTreeView(List<TreeViewContinents> treeViewList)
{
    foreach (var item in treeViewList)
    {
        if (item.Children.Count > 0)
        {
            item.Children = SortTreeView(item.Children);
        }
    }
    return treeViewList.OrderBy(it => it.Content).ToList();
}
topItems = SortTreeView(topItems);