2

I have a linq expression which returns a list of objects. I want to dump that output to console in a pretty tabular format.

is there a way this can be achieved?

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
gargmanoj
  • 123
  • 9
  • I've edited your question, see why: http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – Arin Ghazarian Jan 09 '14 at 11:11

1 Answers1

5

I like to use JSON.NET for serializing objects to string (in pretty tabular format). I also like to create extension method Dump() which outputs serialized object to console:

public static void Dump(this object value)
{
     Console.WriteLine(JsonConvert.SerializeObject(value, Formatting.Indented));
}

Usage is simple:

items.Dump();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thansk a lot Sergey. Can I use the same method with a datatable as well?? – gargmanoj Jan 09 '14 at 11:21
  • @gargmanoj yes, it will serialize DataTable also into array of objects like this `[ { "Column1": Value1, "Column2": Value2 }, { "Column1": Value3, "Column2": Value4 }]` – Sergey Berezovskiy Jan 09 '14 at 11:24
  • I am getting an exception "Self referencing loop detected for property 'Table' with type 'System.Data.DataTable'. Path 'Columns[0]'." when i use it with datatable.. using as dt.Dump(); – gargmanoj Jan 09 '14 at 11:26
  • @gargmanoj I think you need to add loops ignoring, like described here http://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected – Sergey Berezovskiy Jan 09 '14 at 11:28