Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare two objects with more than one list property #58

Open
huangyy21 opened this issue Feb 22, 2024 · 8 comments
Open

Compare two objects with more than one list property #58

huangyy21 opened this issue Feb 22, 2024 · 8 comments

Comments

@huangyy21
Copy link

Hi,
Form example4, I have known how to compare objects with one list.
But if my object have more than one list, what should I do?

@huangyy21
Copy link
Author

I just tried to implement custom Comparer for every list property and it works.
Is there any better way?

@ValeraT1982
Copy link
Owner

Are types of the lists same? Can you give an example?

@huangyy21
Copy link
Author

huangyy21 commented Feb 26, 2024

No, the types are different, but all have an id property.
For example, the demo class below.

public class Demo
{
    public string Name { get; set; }

    public List<A> ItemA { get; set; }

    public List<B> ItemB { get; set; }
}

public class A
{
    public int Id { get; set; }

    public string AA { get; set; }
}

public class B
{
    public int Id { get; set; }

    public int BB { get; set; }
}

@huangyy21
Copy link
Author

I create a class to implement custom Comparer making use of generics, and only have to add if statement in ComparersFactory for every type of lists.

@ValeraT1982
Copy link
Owner

@huangyy21 you can inherit both classes from the same interface and use this interface in ComparersFactory instead of having separate if for each class.

@reponemec
Copy link

In my PR #47, you don't need to implement custom comparer at all. All you need to do is this:

var settings = new ComparisonSettings();
settings.ConfigureListComparison(compareElementsByKey: true, compareUnequalLists: true);
var comparer = new Comparer<Formula>(settings);
var isEqual = comparer.Compare(formula1, formula2, out var differences);

The complete test method: https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Examples/Example4/Example4Tests_BuiltInKeyComparison.cs#L119

@huangyy21
Copy link
Author

In my PR #47, you don't need to implement custom comparer at all. All you need to do is this:

var settings = new ComparisonSettings();
settings.ConfigureListComparison(compareElementsByKey: true, compareUnequalLists: true);
var comparer = new Comparer<Formula>(settings);
var isEqual = comparer.Compare(formula1, formula2, out var differences);

The complete test method: https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Examples/Example4/Example4Tests_BuiltInKeyComparison.cs#L119

Thanks for reply. My memberPath needs more information, so it may be better to implement custom comparer.

@reponemec
Copy link

My memberPath needs more information, so it may be better to implement custom comparer.

Absolutely, however the memberPath property can also be customized using ComparisonSettings:
https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Tests/DifferenceConfigurationTests.cs#L98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants