Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
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
Comments
Philippe Mori 29-Aug-16 17:05pm    
You should show an example of lists and the expected answer.

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();

Hope this solves your Problem
 
Share this answer
 
Comments
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.
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
 
Share this answer
 
Comments
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    
Use Reply widget to post reply.
There's a simpler way. Check my solution after update.
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>();
	pointsA.Add(new Point(5,5));
	pointsA.Add(new Point(10,8));
	
	List<Point> pointsB = new List<Point>();
	pointsB.Add(new Point(5,5));
	pointsB.Add(new Point(9,8));
	
	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.
 
Share this answer
 
v3
Comments
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!
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
		}
	}
 
Share this answer
 
Comments
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    
Your code is assuming:
- 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...
List<int> equalVal = new List<int>();
equal.AddRange(list1.FindAll(x => list2.Contains(x)));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900