question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot compare collections with items of different types

See original GitHub issue

The library doesn’t work correctly in case when you compare lists with items of different types. You are getting an exception like the following:

System.Exception : Invalid CollectionMatchingSpec.  No such property XXXXProperty for type YYYYYClass 
   at KellermanSoftware.CompareNetObjects.IgnoreOrderTypes.IgnoreOrderLogic.GetMatchIndex(ComparisonResult result, List`1 spec, Object currentObject)
   at KellermanSoftware.CompareNetObjects.IgnoreOrderTypes.IgnoreOrderLogic.CompareInOrder(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.IgnoreOrderTypes.IgnoreOrderLogic.CompareEnumeratorIgnoreOrder(CompareParms parms, Boolean countsDifferent)
   at KellermanSoftware.CompareNetObjects.TypeComparers.ListComparer.CompareType(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.RootComparer.Compare(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.TypeComparers.PropertyComparer.PerformCompareProperties(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.TypeComparers.ClassComparer.CompareType(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.RootComparer.Compare(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.IgnoreOrderTypes.IgnoreOrderLogic.CompareInOrder(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.IgnoreOrderTypes.IgnoreOrderLogic.CompareEnumeratorIgnoreOrder(CompareParms parms, Boolean countsDifferent)
   at KellermanSoftware.CompareNetObjects.TypeComparers.ListComparer.CompareType(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.RootComparer.Compare(CompareParms parms)
   at KellermanSoftware.CompareNetObjects.CompareLogic.Compare(Object object1, Object object2)

If you set IgnoreCollectionOrder = true it works better but in some situations you also receive the exception below.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
GregFinzercommented, Dec 16, 2016

You will need to specify a collection matching spec for both types:

    [Test]
    public void CompareListsIgnoreOrderTwoDifferentTypes()
    {
        List<Person> list1 = new List<Person>();
        list1.Add(new Person() {ID = 1, Name = "Logan 5"});
        list1.Add(new Person() { ID = 2, Name = "Francis 7" });

        List<Officer> list2 = new List<Officer>();
        list2.Add(new Officer() { ID = 2, Name = "Francis 7" });
        list2.Add(new Officer() { ID = 1, Name = "Logan 5" });

        ComparisonConfig config = new ComparisonConfig();
        Dictionary<Type, IEnumerable<string>> collectionSpec = new Dictionary<Type, IEnumerable<string>>();
        collectionSpec.Add(typeof(Person), new string[] { "ID" });
        collectionSpec.Add(typeof(Officer), new string[] { "ID" });

        config.IgnoreObjectTypes = true;
        config.IgnoreCollectionOrder = true;
        config.CollectionMatchingSpec = collectionSpec;

        CompareLogic compareLogic = new CompareLogic(config);
        var result = compareLogic.Compare(list1, list2);
        Assert.IsTrue(result.AreEqual);
    }
1reaction
GregFinzercommented, Jul 10, 2021

@PalashBansal Sorry the Compare .NET Object Library is not going to work for something like this. You will need to write your own comparison code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Cannot compare elements of type 'System.Collections. ...
Cannot compare elements of type 'System.Collections.Generic.ICollection`1 Only primitive types, enumeration types and entity types are supported.
Read more >
Compare-Object (Microsoft.PowerShell.Utility)
The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the...
Read more >
5 PL/SQL Collections and Records - Database
PL/SQL lets you define two kinds of composite data types: collection and record. ... You cannot compare associative array variables to the value...
Read more >
C# – Cannot compare elements of type 'System.Collections ...
C# – Cannot compare elements of type 'System.Collections.Generic.ICollection`1 Only primitive types, enumeration types and entity types are supported.
Read more >
Cannot compare incompatible operands of type List ...
Hi ,. How can I overcome this error. Expression evaluation error : Cannot compare incompatible operands of type List of Variant and type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found