Introduction
Starting in Visual Studio 2022 version 17.11, you can directly change the Expression textbox for the IEnumerable visualizer with a modified LINQ expression or by asking a question about the data when at a breakpoint.
By providing this functionality, a developer can ask various questions about a variable in a debugging session rather than stopping debugging and writing another LINQ/Lambda statement.
Taking the time to use this feature can save time and asking a question rather than writing code can teach a developer a new technique.
Important
Since there are decent examples in the Microsoft docs, the following are intermediate to advanced, depending on the reader's expertise in C#.
Example 1: EF Core
In this example, data is read from a SQL Server database using EF Core, and a value converter is used for one property, which is of type enumeration.
Here we ask for all wines in the database, hit a breakpoint and in the Visual Studio local window select wines variable and click view.
This presents the IEnumerable visualizer. Next type "get all records where Models.WineType.White" followed by pressing ENTER. This will write out the code and present white wines only.
The other method is to type in the code, which provides Intellisense as the developer types in the code, followed by pressing ENTER.
Next an order by and asking for White wines. Click the two stars on the right of the visualizer, type "order by Name and where Models.WineType.White" and press ENTER.
Unable to evaluate expression, An error occurred
After attempting to ask a question like “order by Name and where Models.WineType.White” and receiving this message, retry as the question may work if it’s a valid question for the data.
Example 2: Using interface
For this example, several interfaces and three classes are used, which implement the interfaces.
💡 Rather than hand-code or use Bogus NuGet package, ChatGPT was used, see the class.
Here the task is to display the visualizer asking for BirthDate.Year between two dates using the following language extension.
In the following example, where the people variable contains both Person and Customer classes, ask for Customers only.
The next example filters for Customers with more than one Address.
Source code
Example 1 source code Example 2 source code Prompts
Summary
Microsoft Visual Studio’s debugger features, like the IEnumerable visualizer, make the debugger the best bar none.
Spend time with your projects to take the visualizer for a test spin to become familiar with the visualizer, as it may very well save you time with a debugging session.
Top comments (0)