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

3

25

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.

Spillman answered 22/10, 2015 at 9:54 Comment(4)
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 interpretDynamometer
Doesn't vs2015 allow debugging of lambdas in the watch window? dirkstrauss.com/… Also says here you can use the watch / immediate window.Duro
@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)...Spillman
@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.Befall
D
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

Duro answered 22/10, 2015 at 10:2 Comment(2)
Not much luck in finding anything as of yet - still on the lookout though!Duro
Please note that it does not work in managed compatibility modeChaetognath
C
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

Coates answered 22/10, 2015 at 10:1 Comment(10)
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.Spillman
Unfortunately nothing I am aware of. Visual Studio 2015 was released on July 20th 2015.Coates
Note that Visual Studio 2015 Community Edition has exactly the same features as Pro (like that you can install plugins) and is completely free.Gangrene
@RoyT. I've been warned that there are certain issues 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?Spillman
@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? :)Gangrene
@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.Spillman
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"Noninterference
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?Prior
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".Prior
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_EnumerableDebugView1[System.String].get_Items() calls into native method System.Func2[System.Type,System.Collections.Generic.IEnumerable1[System.String]].Invoke(). Evaluation of native methods in this context is not supported.`Prior
G
0

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.

Gangrene answered 22/10, 2015 at 9:57 Comment(3)
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.)Spillman
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<T>.Gangrene
I don't know why the foreach statement doesn't work. That is indeed strange!Gangrene

© 2022 - 2024 — McMap. All rights reserved.