Is there a way to change the dump (header) for a dictionary ?
I've got a Dictionary<string,string> and when I use the dump() before to export the results (Word, Excel), It always show as headers two lines :
- Dictionary<string,string> (N items)
- Key Value
Is there a way to change this display (for example with a .ToDump() method) and how ?
Comments
-
Could you clarify how the final display should look like?
E.g., you can dump directly to grid:
var d = new Dictionary<string,string> { ["k1"] = "v1", ["k2"] = "v2", ["k3"] = "v3" }; d.Dump("", toDataGrid: true);
-
As a simple example, i'd like to customize the header, and especially the Key/Value text
-
Replacing the Key/Value should be simple as long as you don't want to use spaces ( or other characters that C# does not allow in identifiers or start with a number)
You can just use
d.Select(e => new { Classes = e.Key, Propriétés = e.Value}).Dump();
Replacing the header isn't as easy. You can get something that looks like
by using
d.Select(e => new { Classes = e.Key, Propriétés = e.Value }).Dump("Tableau de référencement des classes et des Propriétés");
You could use the tricks from https://forum.linqpad.net/discussion/3262/is-there-a-simple-way-to-hide-object-header-display-from-dump to remove the blue header but see the caveats in that thread.
The only way I know to get output like in your picture is a hack to use regex to parse the html produced by the dump command and replace the header. That obviously means that a LinqPad update could break the hack which may not be something you want. (I have done this in the past with a program that is running on a server and referenced LinqPad to create a free html tables that would be sent to myself via email. But because I was not running LinqPad directly, I knew it would not be updated and possibly break my hack)
If you want I can upload the script but it was just something thrown quickly thrown together.
-
The only way I know to get output like in your picture is a hack to use regex to parse the html produced by the dump command and replace the header.
-
To customize key/values formatting you have to wrap them in class/record and define your own
ToDump
implementation. See https://forum.linqpad.net/discussion/comment/8562/#Comment_8562 for details. -
That make the job for the dictionary title...like the replace trick from here : https://forum.linqpad.net/discussion/3262/is-there-a-simple-way-to-hide-object-header-display-from-dump but for the headers when I export to Word or Excel with formatting, I've got all those headers again...Here is another example :
-
@i2van said:
The only way I know to get output like in your picture is a hack to use regex to parse the html produced by the dump command and replace the header.
I would agree in general, but I thought HtmlAgilityPack was overkill.
-
I didn't want an HTML hack...I'd prefer a Linqpad trick/option to avoid those (unnecessary) headers in Linqpad AND in Word/Excel export from Linqpad. It's great to do it in Linqpad but my goal is to render the same with Export for my documentations. I already use the ToDump() as you can see in my previous capture, but is there an option for the headers ?
-
@BCinU said:
That make the job for the dictionary title...like the replace trick from here : https://forum.linqpad.net/discussion/3262/is-there-a-simple-way-to-hide-object-header-display-from-dump but for the headers when I export to Word or Excel with formatting, I've got all those headers again..The trick (which is basically setting a html hidden style) won't work with the export to excel routines.
The only way around that would be to use the bigger/flaky hack to actually find the headers and remove them completely from the generated html before the export to excel process.
-
It will be great if Jo could add an option to hide it (no html generation) when needed
-
Actually, the request would be something along the lines of "support using a
DisplayNameAttribute
on a property (field?) when renderingDump
output". Outside of this, you're on your own for creating the dumped output (or massaging the output ofUtil.ToHtmlString
).