Diving Into Visual Studio 2015: Debugging Improvements (Tool Window Support for LINQ and Lambda) - Day Six

Before reading this article, I would recommend reading the following previous parts,

Introduction

This article is the continuation part of Debugging Improvements that were explained in Day Five of the series. In the earlier part of the series we covered topics like breakpoint configuration improvements and new improved error list in Visual Studio 2015.This article will cover another debugging improvement of Visual Studio 2015 i.e. tool window support for LINQ and Lambda expressions.

Debugging

Prerequisites

Visual Studio 2015 Express has been used in this tutorial to explain the concepts and features. For samples and practice, a Visual Studio solution is created having a console application named VS2015ConsoleApplication.The console application contains a MyProduct class containing product as an entity specific basic operations like fetching the product and returning the list of products as shown below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace VS2015ConsoleApplication  
  8. {  
  9.     public class MyProducts :IProducts  
  10.     {  
  11.         List<Product> _allProduct = new List<Product>();  
  12.         public MyProducts()  
  13.         {  
  14.             _allProduct.Add(new Product {ProductCode="0001",ProductName="IPhone",ProductPrice="60000",ProductType="Phone",ProductDescription="Apple IPhone" } );  
  15.             _allProduct.Add(new Product { ProductCode = "0002", ProductName = "Canvas", ProductPrice = "20000", ProductType = "Phone", ProductDescription = "Micromax phone" });  
  16.             _allProduct.Add(new Product { ProductCode = "0003", ProductName = "IPad", ProductPrice = "30000", ProductType = "Tab", ProductDescription = "Apple IPad" });  
  17.             _allProduct.Add(new Product { ProductCode = "0004", ProductName = "Nexus", ProductPrice = "30000", ProductType = "Phone", ProductDescription = "Google Phone" });  
  18.             _allProduct.Add(new Product { ProductCode = "0005", ProductName = "S6", ProductPrice = "40000", ProductType = "Phone", ProductDescription = "Samsung phone" });  
  19.   
  20.         }  
  21.   
  22.         /// <summary>  
  23.         /// FetchProduct having price greater that 3000  
  24.         /// </summary>  
  25.         /// <returns></returns>  
  26.         public List<Product> FetchProduct() => (from p in _allProduct where Convert.ToInt32(p.ProductPrice) > 30000 select p).ToList();  
  27.   
  28.         /// <summary>  
  29.         /// FetchProduct  
  30.         /// </summary>  
  31.         /// <param name="pCode"></param>  
  32.         /// <returns></returns>  
  33.         public Product FetchProduct(string pCode)  
  34.         {  
  35.             return _allProduct.Find(p => p.ProductCode == pCode);  
  36.         }  
  37.   
  38.         /// <summary>  
  39.         /// FetchProduct with productCode and productName  
  40.         /// </summary>  
  41.         /// <param name="productCode"></param>  
  42.         /// <param name="productName"></param>  
  43.         /// <returns></returns>  
  44.         public Product FetchProduct(string productCode, string productName)  
  45.         {  
  46.             return _allProduct.Find(p => p.ProductCode == productCode && p.ProductName==productName);  
  47.         }  
  48.   
  49.         public List<Product> GetProductList()  
  50.         {  
  51.             return _allProduct;  
  52.         }  
  53.     }  
  54. }  
where IProducts is a simple interface.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace VS2015ConsoleApplication  
  8. {  
  9.     interface IProducts  
  10.     {  
  11.         Product FetchProduct(string productCode);  
  12.         Product FetchProduct(string productCode,string productName);  
  13.         List<Product> GetProductList();  
  14.     }  
  15. }  
In the following Program.cs file the FetchProduct() method is called to get the list of all the products.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace VS2015ConsoleApplication  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main()  
  12.         {  
  13.             var myProducts = new MyProducts();  
  14.             var allProducts = myProducts.GetProductList();  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
Tool window support for LINQ and lambda expressions

This is one of the best improvements that Visual Studio 2015 has come up with. In earlier versions of Visual Studio when a developer tries to execute/write LINQ or Lambda expressions in immediate window or watch window, the code was not supported and a message used to appear that LINQ and lambda expressions are not allowed in immediate or watch window. With the newly released product i.e. Visual Studio 2015, this limitation has been taken care of. Now a developer can take the liberty to execute LINQ and Lambda expressions in immediate window. This feature proves to be very helpful in run time debugging the code, one can write LINQ queries in immediate windows at run time to select or filter the lists or objects. Let’s cover the topic through practical examples. We have already a solution with a console application that fetches a list of Products. To practically check this tool support feature, place a breakpoint at Console.ReadLine() in program.cs i.e. when the list is fetched.

code

When we hover and try to see all products we get list of products as shown in below image.

code

Now suppose there is a situation where developer wants to perform certain operations for debugging at this breakpoint like checking the product name, product price of each product entity or needs to see products only having price greater than 30000, then a developer has to explicitly navigate through each list item in the window opened as shown below.

code

The above mentioned methodology for debugging and navigating is quite time consuming. Think about list having 1000’s of records. One can leverage the support of LINQ and Lambda expressions in immediate window to expedite the debugging requirements now. Suppose one has a need to get list of all the ProductName in the list. let’s see how we do this in immediate window.

Open the immediate window. You can open it through Visual Studio Debug menu as shown below. I.e. Debug->Windows->Immediate or you can use the keyboard default shortcut to open the window i.e. Ctrl+Alt+I.

window

The immediate window will open at bottom of the class file and you can view the products list by typing allProducts in the window while the code execution is paused at breakpoint. You’ll get the list of products as we saw while hovering the mouse over allProducts variable.

code

Now let’s write a LINQ query to get all product names from the list of products and press enter. Notice that a list of ProductName is fetched through this LINQ query immediately.

(from p in allProducts select p.ProductName).ToList();

code

Likewise you can also perform filtering over the list or execute any LINQ query that you need to execute for the sake of debugging.

In earlier versions LINQ was not supported in immediate window. Now let’s try this with lambda expression.
allProducts.Select(p=>p.ProductName).ToList();

We get the same result.

result

Conclusion

In this part of the Visual Studio 2015 series, we covered immediate window and Watch window support for LINQ and lambda expressions in Visual Studio. This is an extremely useful feature for debugging the collections or objects. We’ll cover more debugging improvement features in upcoming parts. You can also use LINQ and lambda in the watch window.

For more technical articles you can reach out to my personal blog, CodeTeddy.


Similar Articles