1

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
  • 4,665
  • 3
  • 20
  • 21
gargmanoj
  • 97
  • 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

4

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
  • 224,436
  • 37
  • 411
  • 441
  • 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