24

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.

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • I'm pretty sure you can't do it in the immediate window or quick watch either. My 100% guess is that it's to complex for the runtime compiler to interpret – DotNetRussell Oct 22 '15 at 09:57
  • Doesn't vs2015 allow debugging of lambdas in the watch window? http://www.dirkstrauss.com/debugging-lambda-expressions-in-visual-studio-2015/#.ViizHH6rSHv Also says [here](https://www.visualstudio.com/en-us/news/vs2015-vs.aspx#Debug) you can use the watch / immediate window. – Ric Oct 22 '15 at 09:58
  • @StasIvanov Would you mind pointing out the part that's duplicating my question, please? I fail to see the similarity (except for some words that are used)... – Konrad Viltersten Oct 22 '15 at 12:02
  • @KonradViltersten I thought your question was about executing lambdas in debug/immediate/quick watch windows. And it has been already answered in the mentioned question. Also there is an answer that you can actually do it in VS2015, but not in earlier versions. But maybe I misunderstood your question. – Stas Ivanov Oct 23 '15 at 07:24

3 Answers3

12

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

Ric
  • 12,855
  • 3
  • 30
  • 36
8

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

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • Any luck for VS 2013? Most people are using it now and VS 2015 isn't yet officially released, as far I know. And sometimes one can't choose the version because the client calls the shots. – Konrad Viltersten Oct 22 '15 at 10:14
  • Unfortunately nothing I am aware of. Visual Studio 2015 was released on July 20th 2015. – Jamie Rees Oct 22 '15 at 10:15
  • 2
    Note that Visual Studio 2015 Community Edition has exactly the same features as Pro (like that you can install plugins) and is completely free. – Roy T. Oct 22 '15 at 11:32
  • @RoyT. I've been warned that there are [certain issues](http://stackoverflow.com/a/32265725/1525840) in VS15 still. In my experience, RTM of today is not a ready version. SP1 is. :) Having said that, what's the point of getting a payable VS if the community edition has the same features? – Konrad Viltersten Oct 22 '15 at 11:58
  • @KonradViltersten at my work we've all migrated. Of course there are some small issues, but there were also a few of those in 2013. The link you provided doesn't really list a specific issue (wrong link?) anything holding you back? :) – Roy T. Oct 22 '15 at 12:19
  • @RoyT. The comment in the reply linked to mentions that the command isn't available in VS15 (keeping the tab open/promoting current peek window to document). It's not a show stopper but it's one (of many?) small features that I enjoy to have. (Of course, I would also enjoy LINQing in Immediate Window but I'm greedy and want it all.) As for the SP1 as the first usable version, it was a bit of a joke. However, note the tense in your comment - there **were** small issues with VS13. (Yes, and at that time, I was using VS10, hehe.) I'll bring to my PL that we want to upgrade to VS15, though. – Konrad Viltersten Oct 22 '15 at 13:27
  • Just to clarify, if you want to use "Select" as above in the immediate window, the source code that hosts the "things" variable needs to have "using System.Linq" at the top. If it doesn't you will get an error that says "things doesn't contain a definition for Select" – samneric Jan 21 '16 at 20:48
  • Well I'm using VS2015 and still getting the "Expression cannot contain lambda expressions" error in both the watch and immediate windows. Even writing it this way doesn't help: `System.Enumerable.Select(things, thing => thing.Id)` so it's not a scoping issue. Is there some special service pack needed for VS2015 to support Lambda debugging? – Sphynx Jul 21 '17 at 14:57
  • It turns out my Visual Studio settings were to blame; as Maxence mentioned, one needs to go to: "Tools" -> "Options" -> "Debugging" -> "General" and uncheck "Use Managed Compatibility Mode". – Sphynx Jul 21 '17 at 15:17
  • Also, if you get the following error when trying to use a lambda in the immediate window, try calling `ToArray()` on the result set you want to return. `Error: Evaluation of method System.Linq.SystemCore_EnumerableDebugView`1[System.String].get_Items() calls into native method System.Func`2[System.Type,System.Collections.Generic.IEnumerable`1[System.String]].Invoke(). Evaluation of native methods in this context is not supported.` – Sphynx Jul 21 '17 at 15:26
-1

Unfortunately it seems impossible to use lambda's from either the immidiate window or the watch window. The technical reason for this is probabaly that linq queries are usually converted to normal expressions and that somehow this requires a full compilation step instead of the trickery used by these two windows.

In case you didn't know the thing=>thing.Id part is a lambda expression.

Roy T.
  • 9,429
  • 2
  • 48
  • 70
  • Uhm... Yes, I did know what lambda expression is. It's just that I managed to execute LINQ queries **excluding** the lambies (well, not sure if *.First()* - note the empty parentheses really counts as LINQ, but still). Also, your reply doesn't account for the *foreach* issue. (NB I didn't downvote.) – Konrad Viltersten Oct 22 '15 at 10:08
  • 3
    `First()` does not contain a lambda and is not a Linq query. `First()` is defined as an extension method for all types that implement `IEnumerable`. – Roy T. Oct 22 '15 at 11:33
  • 1
    I don't know why the `foreach` statement doesn't work. That is indeed strange! – Roy T. Oct 22 '15 at 11:33