Lists

All methods are under the Perigee.Extensions namespace

MatchToList

This handy method uses several de-duplication methods, as well as the Soundex and Levenshtein algorithms to produce a best-guess match between two different lists.

It will always match A => B. Meaning items that items in A will only attempt matches to items in B, not the other way around.

In the below example, Peech is matched to PeachJuice even though it's misspelled and Agave has no match. Every other item is matched with it's equivalent juice.

List<string> Fruits = new List<string>()
{
    "Apple",
    "Banana",
    "Pear",
    "Orange",
    "Pineapple",
    "Strawberry",
    "Blueberry",
    "Raspberry",
    "Blackberry",
    "Watermelon",
    "Cantaloupe",
    "Grapefruit",
    "Grape",
    "Lemon",
    "Lime",
    "Mango",
    "Papaya",
    "Peech",
    "Agave"
};

List<string> FruitJuices = new List<string>()
{
    "AppleJuice",
    "BananaJuice",
    "PearJuice",
    "OrangeJuice",
    "PineappleJuice",
    "StrawberryJuice",
    "BlueberryJuice",
    "RaspberryJuice",
    "BlackberryJuice",
    "WatermelonJuice",
    "CantaloupeJuice",
    "GrapefruitJuice",
    "GrapeJuice",
    "LemonJuice",
    "LimeJuice",
    "MangoJuice",
    "PapayaJuice",
    "PeachJuice"
};

var match = Fruits.MatchToList(FruitJuices);
Console.WriteLine("Matched:");
match.Item1.ForEach(s => Console.WriteLine($"{s.Key} => {s.Value}"));
Console.WriteLine(Environment.NewLine + "Unmatched:");
match.Item2.ForEach(s => Console.WriteLine($"{s}"));

MatchToList (Dictionary)

This version of list matching is almost identical to what is listed above, the main difference is that your input dictionary defines the keys to match, and the value is a comma separated list of additional "hints" to match by.

In this example, we can match the key Apple with Appljuise, because of the additional hints supplied by the dictionary.

Notice we also supplied 3, true?

  • 3 Means that our distance should be less than or equal to 3 edits. There are some additional preprocessing that occurs to attempt to reduce prefix and postfix content, this distance is applied and checked after.

  • true means MonoMatch is turned on, disallowing the same "list" item to be matched with multiple Keys from my dictionary. If this was false, you could have the same list item matched with as many keys as it matched!

Dictionary<string, string> InList = new Dictionary<string, string>() { 
    { "Apple", "AppleJuice,Apple Juice,jus de pomme,jugo de manzana" } 
};

var rs = InList.MatchToList(new List<string>() { 
"Peach Juice", "Banana Juice", "Appljuise", "mangojuce" }, 3, true);

foreach (var m in rs.Item1)
    Console.WriteLine($"{m.Key} Matched with {m.Value}");
    
//Will print: Apple Matched with Appljuise

MatchToList_Quality

If you're attempting to debug your list matching and want to understand the match values being returned by this, use the _Quality function. It will return a rich object that is easy to read showing the matched value for each key.

JoinBy

Takes every item in a list and joins it with the delimiter

string joined = Fruits.JoinBy(",");

DistinctBy

Works identically to the classic LINQ Distinct, however it works on classes as well.

Items.DistinctBy(f => f.SomeProperty);

ToDataTable

Sometimes you have a list of classes, and need to convert it to a table. This uses reflection to perform the conversion.

ToDataTable(this List<T> items, string tableName = "table", string schema = "dbo")

WhereSubclassOfType<T>

Enable you to filter a list of classes that conform to the subclass of another class

ListOfClasses.WhereSubclassOfType(typeof(otherClass))

Flatten<T,R>

Recursively flatten a data structure and return the new type

ListOfX.Flatten(f => f.otherList)

FromHierarchy<T>

Recursively select down a tree until something is null. Great for returning a list of all inner exceptions in order.

thrownException.FromHierarchy(f => f.InnerException);

RemoveDuplication

Our first example was a bunch of fruit juices. The remove duplication method attempts to find any common pattern in the list of items and remove them.

// Strips the "Juice" from the end of each item
FruitJuices.RemoveDuplicationOfCommonOccuranceSuffix(); 

//There's also a prefixed version
FruitJuices.RemoveDuplicationOfCommonOccurancePrefixes();

Last updated