English 中文(简体)
复杂的印刷
原标题:Complex Dictionary Print

我设立了一个复杂的词典。

mainDict<mainKey, SubDict>
subDict<subKey, sub1Dict>
sub1Dict<sub1Key, sub2Dict>
sub2Dict<sub2Key, sub2Value>
.......

如何将其印成像(vs2005 &net 2.0基)

mainKey/subKey/sub1Key/sub2Key, sub2Value

是否需要将主编改为名单,并加入(a)项?

谢谢。

最佳回答
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<mainKey, SubDict> pair1 in mainDict)
foreach (KeyValuePair<subKey, sub1Dict> pair2 in pair1.Value)
foreach (KeyValuePair<sub1Key, sub2Dict> pair3 in pair2.Value)
foreach (KeyValuePair<sub2Key, sub2Value> pair4 in pair3.Value)
{
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
        pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value);
}

最新资料:

StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, object> pair1 in mainDict)
foreach (KeyValuePair<string, object> pair2 in (Dictionary<string, object>)pair1.Value)
foreach (KeyValuePair<string, object> pair3 in (Dictionary<string, object>)pair2.Value)
foreach (KeyValuePair<string, object> pair4 in (Dictionary<string, object>)pair3.Value)
{
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
        pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value);
}
问题回答

see see see 零散的钥匙,然后将钥匙放在其中,等等。 这样,你就可以把 lo变数和中值作为扼杀物。

您可使用LINQ:

var qry = from pair1 in mainDict
          from pair2 in pair1.Value
          from pair3 in pair2.Value
          from pair4 in pair3.Value
          select pair1.Key + "/" + pair2.Key + "/" + pair3.Key
                   + "/" + pair4.Key + ", " + pair4.Value;

foreach(var s in qry) Console.WriteLine(s);

在几分钟后,我最后写了一封信。 在这方面,我们:

        //---------------------------------------------------
        // Get a nested Dictionary, then...
        Console.WriteLine(DictionaryPrint(nestedDictionary));
        //---------------------------------------------------

        private static string DictionaryPrint(Dictionary<object,object> dictionary, string space = "")
        {
            string output = "";
            foreach(KeyValuePair<object,object> entry in dictionary)
            {
                output += space + entry.Key + ": ";
                if (entry.Value is Dictionary<object, object>)
                    output += "
" + DictionaryPrint((Dictionary<object, object>)entry.Value, space + "  ");
                else if (entry.Value is List<object>)
                    output += "
" + ListPrint((List<object>)entry.Value, space + "  ");
                else
                    output += entry.Value + "
";
            }
            return output;
        }

        private static string ListPrint(List<object> list, string space = "")
        {
            string output = "";
            foreach (object entry in list)
            {
                if (entry is List<object>)
                    output += ListPrint((List<object>)entry, space + "  ");
                else if (entry is Dictionary<object, object>)
                    output += DictionaryPrint((Dictionary<object, object>)entry, space + "  ");
                else
                    output += entry + "
";
            }
            return output;
        }

如果你真是使用“智者”的话,你会比你更糟。

JsonConvert.SerializeObject(yourDict, Formatting.Indented);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签