3

I have a varible of type List that is being returned from a call to Entity Framework and a bunch of other processing before being dropped into a variable that is then serialised into JSON etc etc.

It would be really handy if I could grab the data out of each vairable along the way to analyse where things are going wrong (or right for that mater)

For variables with a little data the immediate window is fine but the variable i'm currently playing with has over 1000 lines of data in it that would be much easier to filter if I could get it into a spreadsheet or the like.

I'd rather not pepper my code with Console.WriteLines or other Trace if I can help it.

So is there some trick or extension or simply some code I can type into the immediate or command window do get this done?

I'm thinking that these might be the way to go but it's not quite gelling for me.

data.ForEach(Console.WriteLine); 

or

File.WriteAllLines("C:\temp", data);

Link to the Immediate Window

EDIT

Here are some examples of what is not working

    [Test]
    public void ImmediateWindowTest()
    {
        var data = new List<dynamic> { new { Z = "A", Y = 1 }, new { Z = "B", Y = 2 } };

        // System.IO.File.WriteAllText (@"c:\temp\foo.txt", data);  
        // -- The best overloaded method match for 'System.IO.File.WriteAllText(string, string)' has some invalid arguments

        // System.IO.File.WriteAllLines(@"c:\temp\foo.txt", data); 
        // -- The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])' has some invalid arguments

        // System.IO.File.WriteAllLines(@"c:\temp\foo.txt", data.Select(p=>String.Format("{0}, {1}", p.Z, p.Y)); 
        // -- Expression cannot contain lambda expressions

    }
Peter
  • 7,792
  • 9
  • 63
  • 94
  • 2
    Why don't you add a helper method that converts you data to a `string` or `string[]`? You can write extension methods or directly add a `ToString()` method to your BO and call `data.ToString()` when you want to log. – Patrice Gahide Sep 22 '14 at 08:03
  • 1
    I like your idea Patrice. If this is simply for debugging, then I'd look into doing this. A quick google of dynamic tostring turned up [this SO answer](http://stackoverflow.com/questions/9299286/dynamic-override-of-tostring-using-reflection) (see the bit using extension methods and tweak to handle Lists). This combined with the #DEBUG preprocessor would ensure it only stays in development code and doesn't ever impact other environments. – Mr Moose Sep 22 '14 at 08:50
  • So its looking like I need a helper class/assembly to do this. That's not so bad I guess and would give me a lot of control over the export. I'll give it a go and post results here. – Peter Sep 22 '14 at 21:03

2 Answers2

3

I just tried the Immediate Window and it works providing you have write permission to the output file directory. So if your code is:

var a = "Lawrence\r\nLessig";

Then in the immediate Window use:

File.WriteAllText (@"c:\Users\MyUserLogin\documents\foo.txt", a);

Creates foo.txt containing:

Lawrence
Lessig
Jay Borseth
  • 1,894
  • 20
  • 29
  • Looks like looping isn't allowed in the Intermediate Window. But see: https://github.com/dp0h/VsImmediate http://extendedimmediatewin.codeplex.com/ – Jay Borseth Sep 22 '14 at 06:37
  • And, all of the above links seem to be crap. Next, see: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2049821-debug-lambda-expressions-planned It's coming in Roslyn... – Jay Borseth Sep 22 '14 at 06:54
  • I had to use `System.IO.File.WriteAllText` (no using in the immediate window). – kitsu.eb May 08 '17 at 22:23
0

I work on a commercial Visual Studio extension that can probably help you achieve what you need to do:

If you need to Filter the list, you can use OzCode's Filter. If you want to do a text search on the values of the fields, you can use OzCode's Search. If you'd like to just export the object graph to a file, that isn't yet directly supported, but after doing a Search you will find a JSON file under %TEMP%\OzCode which will contain all the data (up to the depth in the object graph that you've searched).

Omer Raviv
  • 11,409
  • 5
  • 43
  • 82