简体   繁体   English

C#从列表创建字典<Dictionary>

[英]C# Create a Dictionary from List<Dictionary>

I has a list of dictionaries, like: 我有一个字典列表,例如:

dic1.Add("A_key1","A_val11"); dic1.Add("B_key1","B_val11")
dic1.Add("C","C"); dic1.Add("D","D");

dic2.Add("A_key1","A_val21");  dic1.Add("B_key1","B_val21");
dic2.Add("C","C");  dic1.Add("D","D");
dic2.Add("A_key2","A_val22");  dic1.Add("B_key2","B_val22");
dic2.Add("A_key3","A_val23");  dic1.Add("B_key3","B_val23");

List<Dictionary<string,string>> tempList = new List<Dictionary<string,string>>{dic1, dic2};

I need to create a Dictionary from tempList follow rule: key = "A_key*" value, val = "B_key*" value from all original Dictionaries in list. 我需要从tempList遵循规则创建字典:key =“ A_key *”值,val =“ B_key *”值来自列表中所有原始字典。 Eg, In above example: 例如,在上面的示例中:

expectDic = {("A_val11","B_val11"),
              ("A_val21","A_val21"),
              ("A_val22","A_val22"),
              ("A_val23","A_val23")}

Can I resolve it with Linq? 我可以用Linq解决吗?

Your question is unclear so I'm going to make a few assumptions. 您的问题尚不清楚,因此我将作一些假设。 If these assumptions are incorrect, then let me know and ill modify as appropriate. 如果这些假设不正确,请告诉我,并作适当的修改。

Going off your test data, because the keys will be the same, merging the dictionaries will not be possible, hence the list. 取消测试数据,因为键将相同,因此无法合并字典,因此无法列出。

Finally, your test data in itself is incorrect as it throws an exception. 最后,您的测试数据本身不正确,因为它会引发异常。 Thus, I made the assumption that the test data was the following: 因此,我假设测试数据如下:

        Dictionary<string, string> dic1 = new Dictionary<string, string>();
        Dictionary<string, string> dic2 = new Dictionary<string, string>();

        dic1.Add("A_1", "A_val11");
        dic1.Add("B_1", "B_val11");
        dic1.Add("C", "C");
        dic1.Add("D", "D");


        dic2.Add("A_1", "A_val21");
        dic2.Add("A_2", "A_val22");
        dic2.Add("A_3", "A_val23");
        dic2.Add("B_1", "B_val21");
        dic2.Add("B_2", "B_val22");
        dic2.Add("B_3", "B_val23");
        dic2.Add("C", "C");
        dic2.Add("D", "D");


        List<Dictionary<string, string>> tempList = new List<Dictionary<string, string>> { dic1, dic2 };

Here is some code which will work for the given example: 这是一些适用于给定示例的代码:

Dictionary<string, string> expectDic = new Dictionary<string, string>();
        for (int i = 0; i < tempList.Count; i++)
        {
            Dictionary<string, string> onedic = tempList[i];
            while (onedic.Values.ToList().Where(x => x.Contains("A_")).FirstOrDefault() != null)
            {
                expectDic.Add(onedic.Select(x => x.Value).Where(x => x.Contains("A_") && !expectDic.Keys.Contains(x)).First(), onedic.Select(x => x.Value).Where(x => x.Contains("B_") && !expectDic.Values.Contains(x)).First());
                onedic.Remove(onedic.Select(x => x.Key).Where(x => x.Contains("A_")).FirstOrDefault());
                onedic.Remove(onedic.Select(x => x.Key).Where(x => x.Contains("B_")).FirstOrDefault());
            }
        }

However, what you are asking for is very complicated and I would recommend exception handling for the LINQ. 但是,您要的内容非常复杂,我建议对LINQ进行异常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM