Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Debugging lambda expressions with Visual Studio" no longer working?

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.

like image 953
Protector one Avatar asked Apr 17 '19 10:04

Protector one


People also ask

How to debug a lambda expression in Visual Studio 2015 preview?

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.

Can’T Debug LINQ expression contain lambdas?

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.

How do I fix Visual Studio debug mode is not working?

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.

How do I debug lambda expressions with orderdetails?

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.


2 Answers

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.

like image 115
Protector one Avatar answered Oct 17 '22 00:10

Protector one


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.

like image 43
Manotosh Roy Avatar answered Oct 17 '22 01:10

Manotosh Roy