如何在C#中比较两个列表 [英] How to compare two list in C#

查看:175
本文介绍了如何在C#中比较两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有两个包含相同属性的列表。我想比较这两个列表,只返回c中属性的差值。



我尝试了什么:



i已通过谷歌但我没有找到解决方案

解决方案

最简单快捷的方式



 Except方法返回IEnumerable,你需要将结果转换为列表:



  var  firstNotSecond = list1.Except (list2)。ToList(); 



  var  secondNotFirst = list2。除了(list1).ToList(); 



希望这能解决你的问题


在代码下面更改该行



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





结果

10 8

9 8


从这里开始:如何:查找设定差异两个列表之间(LINQ) [ ^ ]



例如:



班级定义

公共类Point 
{
private int x = 0;
private int y = 0;

public Point()
{
//默认构造函数
}

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; }
}

公共覆盖bool Equals(Object obj)
{
//检查空值并比较运行时类型。
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;
}
}





用法:

  void  Main()
{
List< Point> pointsA = new List< Point>();
pointsA.Add( new Point( 5 5 ));
pointsA.Add( new Point( 10 8 ));

列表<点> pointsB = new List< Point>();
pointsB.Add( new 点( 5 5 ));
pointsB.Add( new 点( 9 8 ));

var result = pointsA.Union(pointsB) // union
。除外( //
pointsA.Intersect(pointsB) // common
);


}





结果:

<前lang =text> XY
10 8
9 8





如您所见,结果列表包含唯一的点。


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

解决方案

Easiest and Quick Way

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


var firstNotSecond = list1.Except(list2).ToList();


var secondNotFirst = list2.Except(list1).ToList();


Hope this solves your Problem


change upon code as below that line

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



Result
10 8
9 8


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:

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.


这篇关于如何在C#中比较两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆