Skip to content

Comparing Lists of Different Lengths

Greg Finzer edited this page Aug 29, 2023 · 4 revisions

Lists of items are compared by looping through the enumerators. For performance reasons, it is assumed that the lists are in the same order. If the counts are different the comparison will exit as soon as it reaches the end of one of the lists. Sometimes lists, arrays, and dictionaries are equal to each other but they are in a different order. The Config.IgnoreCollectionOrder property can be used to compare lists that are not only out of order but also of different lengths. There is a huge performance decrease 🐌 when Ignoring the Collection Order. This may be okay if you are comparing a few hundred objects at a time. By default all the simple type properties are used as a key for an object. To minimize the performance impact, define a Config.CollectionMatchingSpec. This defines the key for the object.

Example

CompareLogic logic = new CompareLogic();
logic.Config.IgnoreCollectionOrder = true;

var spec = new Dictionary<Type, IEnumerable<string>>();
spec.Add(typeof(Customer), new string[] { "CustomerId" });
logic.Config.CollectionMatchingSpec = spec;

Note: It can use derived types. See this example: https://github.com/GregFinzer/Compare-Net-Objects/issues/280