In Visual Studio 2015, support for debugging lambda expressions was introduced: https://devblogs.microsoft.com/devops/support-for-debugging-lambda-expressions-with-visual-studio-2015/
However, I've never been able to get this to work in Visual Studio 2017, nor in the new Visual Studio 2019.
In 2019, I get: "Error: Inspecting the state of an object in the debuggee of type System.Reflection.PropertyInfo is not supported in this context".
Did this functionality get removed?
Example: I am debugging code with a variable "tags" that contains an IQueryable
. I want to check the Name
property of every item, so in the Immediate Window or Watch window I write: tags.Select(t => t.Name)
. Then the error.
This is the new feature in Visual Studio 2015 preview that we can debug lambda expressions. Here I explain how to debug a lambda expression in Visual Studio 2015 preview so please use the following procedure. Create a console application with your desired name in Visual Studio 2015. Here my console application name is lambda expression project1.
Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.
To fix the problem change the debug mode as shown in the right hand side of the figure below to Native only, Mixed, or Auto . Side note: I recommend not choosing Mixed unless your system has both managed and native code (code that does not run under the CLR) because Visual Studio can't attach to an already running process in mixed mode.
If you hover your cursor over the orderDetails variable, a tip will be displayed that shows you the returned items are 5. Clicking the push pin will now keep that data tip pinned and always available during your debugging session. If you right click this data tip, you can add a Lambda Expression there too.
This is more of a temporary answer without background until someone with knowledge comes along.
If you call ToArray
on the IQueryable
, you can use lambdas in debugging on that. It doesn't work on the AsEnumerable
result though, so it doesn't simply seem to be about using IEnumerable
methods vs. IQueryable
methods.
As an alternative, you can use an Immediate window to explore the value. Consider the simple code here:
static void Main(string[] args)
{
int[] Numbers = { 10, 20, 30, 40 };
var NewNumbers = Numbers.Select(x => x * x);
}
Put a debug point where your lambda expression is and open the Debug -> Window -> Immediate.
In the Immediate window, you can write the expression you want to test and see the result. I typed:
Numbers.Select(x => x*x)
Press enter, you will see the result as :
Count = 4
[0]: 100
[1]: 400
[2]: 900
[3]: 1600
An Immediate window is a playground to check variables, run expressions, and helps to debug faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With