需要在Linq 中对比两个对象是否相等

/// <summary>
/// 定义一个点
/// </summary>
class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
 List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));

三种对比方法均不能

Point p1 = new Point(, );
Point p2 = new Point(, );
Console.WriteLine(p1 == p2);//False
Console.WriteLine(p1.Equals(p2));//False
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
p1 = p2;
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True

由于没有重写 == 运算符 和 Equals 方法,不能够 直接使用否则对比的将是对象的引用地址

需要对类进行重写,详细如下

   /// <summary>
/// 定义一个点,并重写对象与对象是否相等的方法
/// 可用于判断对象是否相等
/// eg:
/// obj1 == obj2
/// obj1.Equals(obj2)
/// </summary>
class TestPoint : IEquatable<TestPoint>
{
public int x { get; set; }
public int y { get; set; }
public TestPoint(int x, int y)
{
this.x = x;
this.y = y;
} /// <summary>
/// 重载 == 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator ==(TestPoint p1, TestPoint p2)
{
return (p1.x == p2.x) && (p1.y == p2.y);
} /// <summary>
/// 重载 != 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator !=(TestPoint p1, TestPoint p2)
{
return (p1.x != p2.x) || (p1.y != p2.y);
} /// <summary>
/// 重写Equals(object obj)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
return this.Equals(obj as TestPoint);
} /// <summary>
/// 重写 计算对象的哈希值方法(自定义 这里只是示范)
     /// 该方法用于判断对象的哈希值是否相等 如对象哈希值相同 就认为两个对象 相等
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
} /// <summary>
/// 继承定义Equals<T>方法
/// 需要继承接口IEquatable<T>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(TestPoint other)
{
return (this.x == other.x) && (this.y == other.y);
} }

使用大概示范

       Point p1 = new Point(, );
Point p2 = new Point(, );
Console.WriteLine(p1 == p2);//False
Console.WriteLine(p1.Equals(p2));//False
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
p1 = p2;
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True TestPoint p3 = new TestPoint(, );
TestPoint p4 = new TestPoint(, );
Console.WriteLine(p3 == p4);//True
Console.WriteLine(p3.Equals(p4));//True
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
p3 = p4;
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));
List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
var result2 = list2.Where(M => M == new TestPoint(, ));

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
/// <summary>
/// 定义一个点
/// </summary>
class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
} /// <summary>
/// 定义一个点,并重写对象与对象是否相等的方法
/// 可用于判断对象是否相等
/// eg:
/// obj1 == obj2
/// obj1.Equals(obj2)
/// </summary>
class TestPoint : IEquatable<TestPoint>
{
public int x { get; set; }
public int y { get; set; }
public TestPoint(int x, int y)
{
this.x = x;
this.y = y;
} /// <summary>
/// 重载 == 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator ==(TestPoint p1, TestPoint p2)
{
return (p1.x == p2.x) && (p1.y == p2.y);
} /// <summary>
/// 重载 != 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator !=(TestPoint p1, TestPoint p2)
{
return (p1.x != p2.x) || (p1.y != p2.y);
} /// <summary>
/// 重写Equals(object obj)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
return this.Equals(obj as TestPoint);
} /// <summary>
/// 重写 计算对象的哈希值方法(自定义 这里只是示范)
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
} /// <summary>
/// 继承定义Equals<T>方法
/// 需要继承接口IEquatable<T>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(TestPoint other)
{
return (this.x == other.x) && (this.y == other.y);
} }
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(, );
Point p2 = new Point(, );
Console.WriteLine(p1 == p2);//False
Console.WriteLine(p1.Equals(p2));//False
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
p1 = p2;
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True TestPoint p3 = new TestPoint(, );
TestPoint p4 = new TestPoint(, );
Console.WriteLine(p3 == p4);//True
Console.WriteLine(p3.Equals(p4));//True
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
p3 = p4;
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));
List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
var result2 = list2.Where(M => M == new TestPoint(, )); Console.Read();
}
}
}

ReferenceEquals 不能重写 注意

用于工作记录

2018年12月7日13:22:13

lxp

最新文章

  1. PHP中模拟JSONArray
  2. 如何更换centos6源
  3. C++异常处理的问题
  4. vsftpd 修改默认目录
  5. Oracle自定义函数
  6. JavaScript--事件模型(转)
  7. Caroline--chochukmo
  8. Python爬取17吉他网吉他谱
  9. Android canvas rotate():平移旋转坐标系至任意原点任意角度-------附:android反三角函数小结
  10. Struts2 DMI的使用
  11. 采购IC应该知道的十大网站
  12. 基于nginx的rtmp的服务器(nginx-rtmp-module)
  13. follow through
  14. ssm知识点总结
  15. 小程序的POST接收不到参数
  16. Django:模型model和数据库mysql(二)
  17. EasyUI DataGrid Checkbox 多选 获取选中行中的内容
  18. 【Unity】9.1 导入粒子系统组件
  19. PHP设计模式系列 - 迭代器
  20. 使用Apache FtpServer

热门文章

  1. 机器学习作业(三)多类别分类与神经网络——Python(numpy)实现
  2. BK: Data mining: concepts and techniques (1)
  3. solr es调优化和问题排查
  4. 六、JVM之垃圾回收
  5. STL-vector-set_difference B - 人见人爱A-B
  6. Homebrew安装Mysql后的两步必要的命令
  7. python爬虫模拟登录的图片验证码处理和会话维持
  8. 在cc.EventListener.TOUCH_ONE_BY_ONE事件中判断拖动物离哪边近飞向那边
  9. jQuery捕获
  10. k线中转器