15,999,861 members
1.00/5 (1 vote)
See more:
Hi,
i have two lists which contain same property.I want to compare those two lists and return only difference value of property in c#

What I have tried:

i have gone through google but i didnt find solution
Posted
Updated 20-Jan-22 2:33am
Philippe Mori 29-Aug-16 17:05pm
You should show an example of lists and the expected answer.

## Solution 3

Easiest and Quick Way

C#
`The Except method returns IEnumerable, you need to convert the result to list:`

C#
`var firstNotSecond = list1.Except(list2).ToList();`

C#
`var secondNotFirst = list2.Except(list1).ToList();`

Maciej Los 30-Aug-16 1:43am
It should work for list of simple object, such as integer, string, etc. For more complicated objects, such as custom class with set of properties it won't work, because of objects have to expose `Equals()` method.

## Solution 2

change upon code as below that line

C#
`var result = pointsA.Union(pointsB).Where(w => !(pointsA.Contains(w) && pointsB.Contains(w)));`

Result
10 8
9 8

Philippe Mori 29-Aug-16 17:04pm
I think that this code is not very efficient.
Halit Yurttaş 29-Aug-16 17:12pm
I think its not short way or i know so. Sorry
Maciej Los 29-Aug-16 17:23pm
There's a simpler way. Check my solution after update.

## Solution 1

Start here: How to: Find the Set Difference Between Two Lists (LINQ)[^]

For example:

Class definition
```public class Point
{
private int x = 0;
private int y = 0;

public Point()
{
//default constructor
}

public Point(int _x, int _y)
{
x = _x;
y = _y;
}

public int X
{
get { return x; }
set { x = value; }
}

public int Y
{
get { return y; }
set { y = value; }
}

public override bool Equals(Object obj)
{
// Check for null values and compare run-time types.
if (obj == null || GetType() != obj.GetType())
return false;

Point p = (Point)obj;
return (x == p.x) && (y == p.y);
}

public override int GetHashCode()
{
return x ^ y;
}
}```

Usage:
C#
```void Main()
{
List<Point> pointsA = new List<Point>();

List<Point> pointsB = new List<Point>();

var result = pointsA.Union(pointsB) //union
.Except( //except
pointsA.Intersect(pointsB) //common
);

}```

Result:
```X  Y
10 8
9  8 ```

As you can see, a result list contains unique points.

v3
Philippe Mori 29-Aug-16 17:04pm
I think OP only want points that are different...
Maciej Los 29-Aug-16 17:08pm
You might be right, Philippe. Probably OP want to get only 2 points: {(10,8), (9,8)}. I'll improve my answer soon.
[EDIT]
Done!

## Solution 4

You can try something like that:
C++
```for (int i= 1; i < ListA.length; i++) {
if (ListA[i] != ListB[i]) {
// diferent, do what you want
}
}```

Maciej Los 30-Aug-16 1:50am
Well... What if the size of list is differ (for example ListB is longer than ListA)? What if the objects on both lists aren't simple types and does not provide a way to compare them (there' no way to use comparison operators)?
Patrice T 30-Aug-16 4:26am
Well... Comparison of 2 lists is so trivial/basic that the question look like HomeWork, The OP didn't show any code and didn't have the shadow of the first clue of the situation. So I let the OP deal with details.
Luc Pattyn 20-Jan-22 10:26am
- both lists have same length
- both lists have same order
- no duplicate items are present within any of these lists

and the OP did not even start to explain when he considers two lists identical or different...

## Solution 6

```List<int> equalVal = new List<int>();