C# 如何拆分字符串并相对于父字符串创建一个字符串

C# 如何拆分字符串并相对于父字符串创建一个字符串,c#,asp.net,C#,Asp.net,我有以下条件: "Parent1:Child1" "Parent1:Child2" "Parent1:Child3" "Parent1:Child4" "Parent2:Child1" "Parent2:Child2" "Parent2:Child3" "Parent2:Child4" "Parent3:Child1" "Parent3:Child2" 现在我想创建如下字符串: Parent1:Child1,Child2,Child3,Child4 Parent2:Child1,Child

我有以下条件:

"Parent1:Child1"
"Parent1:Child2"
"Parent1:Child3"
"Parent1:Child4"

"Parent2:Child1"
"Parent2:Child2"
"Parent2:Child3"
"Parent2:Child4"

"Parent3:Child1"
"Parent3:Child2"
现在我想创建如下字符串:

Parent1:Child1,Child2,Child3,Child4
Parent2:Child1,Child2,Child3,Child4
Parent3:Child1,Child2

我尝试过许多方法,但没有一种是正确的。有什么帮助吗?

您可以按
拆分每个字符串。 使用
字典
。键将包含父详细信息,值将包含子项

更可读、更容易理解的东西

    public static void Main()
        {
            string[] input = new string[]
                        { "Parent1:Child1", "Parent1:Child2",
                          "Parent1:Child3", "Parent1:Child4", 
                          "Parent2:Child1", "Parent2:Child2", 
                          "Parent2:Child3", "Parent2:Child4", 
                          "Parent3:Child1","Parent3:Child2"
                        };

            Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            foreach(string s1 in input)
            {
                string[] splitted = s1.Split(':');
                List<string> temp = new List<string>();
                if(!dict.ContainsKey(splitted[0]))
                {
                    temp.Add(splitted[1]);
                    dict.Add(splitted[0], temp);
                }
                else
                    dict[splitted[0]].Add(splitted[1]);
            }

            foreach(KeyValuePair<string, List<string>> kv in dict){
                Console.WriteLine(kv.Key+" : "+ string.Join(",",kv.Value));
            }           
        }
publicstaticvoidmain()
{
字符串[]输入=新字符串[]
{“Parent1:Child1”、“Parent1:Child2”,
“家长1:Child3”、“家长1:Child4”,
“Parent2:Child1”、“Parent2:Child2”,
“Parent2:Child3”、“Parent2:Child4”,
“Parent3:Child1”、“Parent3:Child2”
};
Dictionary dict=新字典();
foreach(输入中的字符串s1)
{
string[]splitted=s1.Split(“:”);
列表温度=新列表();
如果(!dict.ContainsKey(拆分为[0]))
{
临时添加(拆分[1]);
dict.Add(拆分[0],温度);
}
其他的
dict[splitted[0]]。添加(splitted[1]);
}
foreach(dict中的键值对kv){
Console.WriteLine(kv.Key+“:”+string.Join(“,”,kv.Value));
}           
}
概念验证:Linq方法

string[] input = { "Parent1:Child1", "Parent1:Child2", "Parent1:Child3", "Parent1:Child4",
                   "Parent2:Child1", "Parent2:Child2", "Parent2:Child3", "Parent2:Child4",
                   "Parent3:Child1", "Parent3:Child2"};
  • 首先,我通过
    拆分()每个项目:
  • 然后I
    GroupBy()
    split结果的第一部分(=“ParentX”)
  • 最后,我用所有孩子的
    Join()
    将每个组键连接起来
代码:


您可以使用以下代码来实现上述结果

List<Child> childdren = new List<Child>
            {
                new Child { ParentName ="Parent1", ChildName ="Child1"},
                new Child { ParentName ="Parent1", ChildName ="Child2"},
                new Child { ParentName ="Parent1", ChildName ="Child3"},
                new Child { ParentName ="Parent1", ChildName ="Child4"},

                new Child { ParentName="Parent2", ChildName ="Child1"},
                new Child { ParentName ="Parent2", ChildName ="Child2"},
                new Child { ParentName ="Parent2", ChildName ="Child3"},
                new Child { ParentName ="Parent2", ChildName ="Child4"},

                new Child { ParentName ="Parent3", ChildName ="Child1"},
                new Child { ParentName ="Parent3", ChildName ="Child2"},

            };


            var ChildByParentName = childdren.ToLookup(x => x.ParentName);
            foreach (var item in ChildByParentName)
            {
                Console.WriteLine(item.Key); // Print Parent Name

                foreach (var child in ChildByParentName[item.Key])
                {
                    Console.WriteLine("\t" + child.ChildName + ", "); // Print Child Names
                }
            }

你能展示一些方法并解释出哪里出了问题吗+1.
List<Child> childdren = new List<Child>
            {
                new Child { ParentName ="Parent1", ChildName ="Child1"},
                new Child { ParentName ="Parent1", ChildName ="Child2"},
                new Child { ParentName ="Parent1", ChildName ="Child3"},
                new Child { ParentName ="Parent1", ChildName ="Child4"},

                new Child { ParentName="Parent2", ChildName ="Child1"},
                new Child { ParentName ="Parent2", ChildName ="Child2"},
                new Child { ParentName ="Parent2", ChildName ="Child3"},
                new Child { ParentName ="Parent2", ChildName ="Child4"},

                new Child { ParentName ="Parent3", ChildName ="Child1"},
                new Child { ParentName ="Parent3", ChildName ="Child2"},

            };


            var ChildByParentName = childdren.ToLookup(x => x.ParentName);
            foreach (var item in ChildByParentName)
            {
                Console.WriteLine(item.Key); // Print Parent Name

                foreach (var child in ChildByParentName[item.Key])
                {
                    Console.WriteLine("\t" + child.ChildName + ", "); // Print Child Names
                }
            }
Parent1
        Child1,
        Child2,
        Child3,
        Child4,
Parent2
        Child1,
        Child2,
        Child3,
        Child4,
Parent3
        Child1,
        Child2,