0

Consider two lists (or other appropriate data structures) of the objects of type:

class Object
{
    public string clause;

    public string preamble;
    public string description;
}

I need to build a list of these objects that are NOT perfect matches (preamble and description exactly the same in both). For example, if there is a record in list A whose preamble matches that of one in list B, but their description do not match, it should be included. If their description match, they should not be included (that would be part of their intersection).

I have been going round in circles trying to find a solution, including implementing nested foreach loops. Is there a symmetric difference function for List, or any other appropriate data structure, that can compare objects by comparing their members like this?

For context, one of the lists represents records from an Excel spreadsheet obtained with Epplus library, and the other list represents issues from Jira API.

Al2110
  • 566
  • 9
  • 25
  • 1
    You'll need to create an appropriate `IEqualityComparer` for your types (which I hope you *haven't* called `Object`) to consider objects with equal preamble and description to be equal. After that, `HashSet.SymmetricExceptWith` is your friend - and if you don't want to use that, you could use `a.Except(b, comparer).Concat(b.Except(a, comparer))`. – Jon Skeet Feb 20 '20 at 06:50

1 Answers1

0

There is no convenient built-in solution for that, probably because object in .NET is not data. However, there are some projects useful for your problem, like Compare .NET Objects, which can compare object by using reflection and can be configured to list all differences between two objects.

tia
  • 9,518
  • 1
  • 30
  • 44
  • How is `HashSet.SymmetricExceptWith` not a convenient built-in solution? You'd need to create an `IEqualityComparer`, but that's all. – Jon Skeet Feb 20 '20 at 06:49
  • "that can compare objects by comparing their members like this?" so I interpret the question as that he has problem detecting the differences between two objects. – tia Feb 20 '20 at 06:55
  • That's half of one sentence - all of the rest (including the title) is about symmetric differences, which you don't talk about at all in the answer. I think it would have been much more appropriate to write an answer explaining how to do this using an `IEqualityComparer`, potentially referring to that project as a way of avoiding having to write that. (But really, for two properties the comparer would be *very* simple to write.) – Jon Skeet Feb 20 '20 at 07:00