How to execute LINQ and/or foreach in Immediate Window in VS 2013?

23,405

Solution 1

According to the new features available in visual studio 2015, support for debugging lambdas is now available in the watch/immediate window:

Lambda Expressions in Debugger Windows

You can now use lambda expressions in the Watch, Immediate, and other debugger windows in C# and Visual Basic.

Source:

Visual Studio 2015 RTM

Solution 2

In VS2015 you can use lambda expressions in the watch window and immediate window.

Just add the watch or type in the immediate window (While debugging and things is in scope):

things.Select(thing => thing.Id);

and you will get a list of results.

Here is a blog about this

Share:
23,405
Konrad Viltersten
Author by

Konrad Viltersten

A self taught code monkey since the age of 10 when I got my first computer, the coolest Atari 65XE. Later on, a mathematics and computer science student at a university with a lot of side-studies in philosophy, history, Japanese etc. Today, a passionate developer with focus on web related technology from UX, through JS/TS to C# with a touch of SQL. Motto: A lousy programmer knows how to create problems. A good programmer knows how to solve problems. A great programmer knows how to avoid them. (Get the double meaning?) Works at: http://kentor.se Blogs at: http://konradviltersten.wordpress.com Lives at: http://viltersten.somee.com

Updated on September 23, 2020

Comments

  • Konrad Viltersten
    Konrad Viltersten almost 4 years

    Immediate Window is fantastically useful tools when probing the current state during debugging process. I learned that by using the question mark, one can do a bit more in there as shown in this post.

    However, I still don't know how to execute LINQ queries there (including lambda expressions). I've also failed to execute a foreach statement.

    When executing the following statements:

    ?(things.Select(thing=>thing.Id);)
    ?(foreach(var thing in things);)
    

    I'm getting these errors:

    Expression cannot contain lambda expressions
    Invalid expression term 'foreach'

    (How) can I execute these in the Immediate Window?

    There's also a tool in VS Gallery but it's said that it only works for VS05 and VS08, which most programmers have left behind looong time ago. I'm looking for something applicable to VS13 and/or VS15.