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?

1

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Thansk a lot Sergey. Can I use the same method with a datatable as well??
@gargmanoj yes, it will serialize DataTable also into array of objects like this [ { "Column1": Value1, "Column2": Value2 }, { "Column1": Value3, "Column2": Value4 }]
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<DataTable>();
@gargmanoj I think you need to add loops ignoring, like described here stackoverflow.com/questions/13510204/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.