Searchapalooza!!! Part 1

The search is on
Learn how Ozcode can be used to search through collections and focus on a subset of records.

Dealing with large collections of data is one of the biggest problems a C# developer can face, and a pretty common occurrence when debugging C# applications. As data science becomes more mainstream, this generalization is unlikely to change too much in the foreseeable future. I often find that my debugging techniques require me to hunt through a couple of thousand records in a debugger in order to isolate the single value that’s breaking some part of my code. Having some way to get at that one problematic record without painstakingly searching through thousands of records would be amazing.

Ozcode is one of my favorite C# debugging tools and one if its many functions is the ability to search through collections to focus on just a subset of records. Let’s take a look at how we can use the features of searching within a collection to make our lives easier.

Setting up

I always like to use real-world scenarios to play with in these demos, so today we’re going to be looking at some open data provided by the City of Calgary, where I live. In particular, we’re interested in what building permits have been issued in the last month or so. Years ago this data wouldn’t have been available to us unless we were willing to pay through the nose. Luckily for us, times have changed and we can simply go here to download a CSV file.

Now we’ll need to load this into a C# application. Reading a CSV is one of those deceptively difficult things to do. It feels like a problem we could solve with a File.ReadAllText and a regular expression. Unfortunately, there are a lot of corner cases in the file format which need to be handled. It is much easier to lean on a library like CSVHelper to handle the loading.

using CsvHelper;
using System;
using System.IO;
using System.Linq;

namespace searchapalooza
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var reader = new StreamReader("Building_Permits.csv"))
            using (var csv = new CsvReader(reader))
            {
                var records = csv.GetRecords<BuildingPermit>().ToList();
              
            }
        }
    }
}

If we set a breakpoint just after loading the records, we can start exploring the data with Ozcode.

Finding records

The first thing we want to do is find any record that is related to one of the up-and-coming communities in the city called Seton.

Searching for Seton
Ozcode Visual Studio Extension - FREE DOWNLOAD

Here, we can see that 1102 records have been loaded at the breakpoint. Looking for Seton by going through these records manually would be a huge pain. Fortunately, we can hover over the collection and enter the text we’re looking for in the search box. You can see that Ozcode is helpful enough to offer us some suggestions for our search. With the search applied, we immediately find that there are 56 places that match our criteria.

If you look down at the bottom right of the search dialog you’ll notice some chevrons

Blue chevrons on the far right

These chevrons indicate how many levels into the object hierarchy the search will explore. The more levels we explore, the more data the search needs to examine, so this defaults to 2 levels. You can explore as many as you like by repeatedly clicking on the chevrons.

Property developers in Calgary suffer from a deplorable lack of imagination, and have the nasty habit of naming streets in new developments very similarly. In Seton, for example, there are roads called Seton Drive, Seton Place, Seton Passage, Seton Way – it’s simply a nightmare to give people directions! This means although there were 56 matches, fewer than those are likely to be in Seton. Let’s see if we can narrow down our results.

Conditions

The field we’re really interested in is the CommunityName. Search will look at all the fields in an object but, since we know exactly which field we want, we can switch the little drop down from Search to Condition. Here we can enter any expression which evaluates to a Boolean. In our case, something like [obj].CommunityName == "SETON"should do the trick.

Debugging c# collections by using conditional evaluation
Using the condition search mode. All this functionality is also available in the QuickWatch screen

Because this field takes a Boolean, we could also have used something like [obj].CommunityName.Contains("SETON", StringComparison.InvariantCultureIgnoreCase)
Conditional provides for more specific searches and multiple conditions to really narrow down your collection. You could even use the condition field to do a search with a regular expression.

Find root cause - Ozcode

Filtering

Filtering collections in the debugger
The filter button

If you cast your eyes up from the depth chevrons, you’ll see a filter button. Clicking on this button hides records that don’t match the search criteria, which reduces visual clutter.

Custom expressions

Sometimes the value we want to search for isn’t one that is represented in the collection, but rather one derived from values in the collection. For instance, let’s imagine we have a method such as this:

        public static bool RequiresReview(BuildingPermit permit)
        {
            return permit.EstProjectCost.HasValue 
                &amp;&amp; permit.EstProjectCost > 20000 
                &amp;&amp; permit.StatusCurrent== "Pre Backfill Phase";
        }

This performs some complex logic on a building permit. The result of this field can be added to a collection in the debugger using a custom expression. It can also be useful when debugging methods, to project the application of this method to an entire collection.

Debugging c# collections by filtering using a custom expression
Searching for a custom expression

The combination of the custom field and the conditional search allows you to search for and see the result of a calculation on the collection.

Moving on

Searching in a collection allows you to quickly narrow down the values that are causing your code to fail. In part 2 of this series, we’ll look at how we can apply some of these techniques to searching through large objects.


Ozcode Visual Studio Extension

Data tips provide deep insights into your data letting you drill down to any level in a data object.

Search. Quickly and effortlessly search member names and values, regardless of the size of the collection or complexity of the object graph.

Conditional search. Search collections using custom predicates; from logical expressions to any complex logic you have in mind.

Reveal. “Star” the properties you are interested in to customize how an object appears in the debugger so you can focus on the data that really matters to you.

Ozcode Visual Studio Extension - Free Download

Simon Timms

Comments

Keep your code
in good shape!

Follow the latest news in the .NET debugging industry

Ready to Dive into Your Prod Code?

Easy debugging with full time-travel data

Share on facebook
Share on twitter

Recent Posts

Follow Us

Join Ozcode YouTube Channel

Let’s start debugging, it’s free!

Keep your code in good shape!

Follow the latest news in the .NET debugging industry
Ozcode Logo

This website uses cookies to ensure you get the best experience on our website.