Debugging Linq in the Visual Studio Command Window

Tags: Visual Studio, Debugging, Linq

Having made the jump from Visual Studio 2013 to Visual 2017.  I missed some features, added to Vs2015, that helped me out the other day while trying to debug an issue with a collection in C#

In Visual Studio 2015 and greater, you can query collections using linq in the command window 

Lets suppose we have the following code.  I know its not the greatest but it should illustrate the point.

 private List<ItemRow> GenerateItems()
            List<ItemRow> items = new List<ItemRow>();
            Random r = new Random();
            const string message = "The Quick Brown Fox Jumped Over the Lazy Dog";

            var total = r.Next(75, 100);
            for (int i = 0; i < total; i++)
                var chosenDate = DateTime.Now.AddDays(r.Next(180));
                items.Add(new ItemRow{
                    Id = i,
                    Name = new string(message.ToCharArray().OrderBy(s=>(r.Next(2) %2) == 0).ToArray()),
                    ChosenDate = new DateTime(chosenDate.Year, chosenDate.Month, chosenDate.Day),
                    Amount = (0.4m * r.Next(50))});

            return items;

If we add a break point on the return items line

we can begin to query the items collection in the command window

We can call a Count

Big Deal!  Okay lets try filter 

There is nothing to stop you using the watch window and moving through the hierachy 

but if you need to see the rows togther it can be useful 

Bear in mind if your collection has more than 100 items you are limited to only the first 100 items and you can experience a slight delay with more complicated queries.

But for a quick data check it can be a little time saver 

No Comments

Add a Comment