0

Assume I have the following object foo and its properties listed below:

class foo
public string name
public string property1
public string property2

Now let's instantiate two lists of foo: List<foo>()

The first list contains two foo objects:

new foo() = 
{
   name = "name",
   property1 = "random value",
   property1 = "random value"
}

new foo() = 
{
   name = "name",
   property1 = "random value",
   property1 = "random value"
}

The second list contains one foo object:

new foo() = 
{
   name = "name",
   property1 = "random value",
   property1 = "random value"
}

Is it possible using LINQ to compare and return a new List<foo> that contains the differences between these two lists (or any other list for that matter)? Taking into consideration that item order and any property value that does not match are also considered as a difference.

This was helpful for primitive objects: Compare two lists, item by item, using linq but does not provide a way to return a new list with the differences.

EDIT

@CSharpie reopened... Also now the question looks very poorly defined - i.e. what is expected result for {1,2,3,4} and {1,3,4}? Possibly OP is looking for some sort of diff..

@AlexeiLevenkov The output should be 2. Or for a list of {1,2,3,4} and {1,3,2, 4} the output should be {3,2} since the order (or values) of 3 and 2 have changed from the original list.

Community
  • 1
  • 1
user1027620
  • 2,745
  • 5
  • 37
  • 65
  • See Linq Set Operations https://code.msdn.microsoft.com/LINQ-Set-Operators-374f34fe – Amitd Jan 27 '16 at 17:24
  • @CSharpie reopened... Also now the question looks very poorly defined - i.e. what is expected result for {1,2,3,4} and {1,3,4}? Possibly OP is looking for some sort of diff.. – Alexei Levenkov Jan 27 '16 at 17:32
  • @Alexei Levenkov, you are right. OP: You need to clarify what you consider as a difference between two lists and also what role the order of the items in those lists play. Best is to specify a frew examples with desired outcomes. – CSharpie Jan 27 '16 at 17:39
  • Sorry guys my bad, will update the question in a bit. – user1027620 Jan 27 '16 at 17:39
  • @CSharpie please see my edit. If it's not clear please let me know. – user1027620 Jan 27 '16 at 17:43
  • Possible duplicate of [Using LINQ to Objects to find items in one collection that do not match another](http://stackoverflow.com/questions/1647698/using-linq-to-objects-to-find-items-in-one-collection-that-do-not-match-another) – Richard Anthony Hein Jan 27 '16 at 20:11
  • @RichardHein The objects are of the same type in this case. (Does this solution take into consideration the order of items as differences?) – user1027620 Jan 27 '16 at 20:37
  • Does the order of the returned results matter? You say 1,2,3,4 diff 1,3,2,4 is 3,2, but I don't see any obvious way to determine the ordering. – 31eee384 Jan 27 '16 at 20:59
  • It doesn't matter, but comparing 1,3,2,4 to the initial list 1,2,3,4 returns that 3 and 2 have been "displaced"... Might also return 1 in that case if the value of 1 changed. – user1027620 Jan 27 '16 at 22:18

1 Answers1

5

Try using the Except LINQ method:

List<int> a = new List<int>{ 1, 2, 5 };
List<int> b = new List<int>{ 2, 3};
var result = a.Except(b).Union(b.Except(a)).ToList();

In set theory, (A-B)+(B-A) is equivalent to the differences between the two of them.

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
sowen
  • 1,090
  • 9
  • 28
  • Does this work for complex objects? Such as `foo` as described above? – user1027620 Jan 27 '16 at 17:59
  • 1
    of course, you just need to write your own Comparor. It's a hint, you need to try it from here – sowen Jan 27 '16 at 18:08
  • another hint is, look into MoreLinq or ExtraLinq, they might have a new extension that meets your need. But even they do, you still need to write a comparor to determine why 2 objects are equal – sowen Jan 27 '16 at 18:10
  • How will this method differentiate the order? – serdar Jan 27 '16 at 20:14