IEqualityComparer的使用
当我们用Linq操作我们自定义的对像数组时,我们会发现有些方法直接使用的话根本不起作用,比如:Distinct、Except、Intersect等扩展方法。
对于我们自定义的对象的比较,我们必须实现IEqualityComparer接口来判断两个对象的相等性。
示例代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace lambda
{
class Program
{
static void Main(string[] args)
{
Park p1 = new Park { ticketPrice = 55, address = "南京", peoples = 85 };
Park p2 = new Park { ticketPrice = 85, address = "北京", peoples = 75 };
Park p3 = new Park { ticketPrice = 78, address = "多伦多", peoples = 100 };
List<Park> parks = new List<Park>(){
new Park { ticketPrice = 11, address = "天堂", peoples = 1000 },
new Park { ticketPrice = 11, address = "天堂", peoples = 1000 }
};
parks.Add(p1);
parks.Add(p2);
parks.Add(p3); var diff = from c in parks.Distinct(new Park()) select c;
foreach (var item in diff)
{
Console.WriteLine(item.address);
}
} } class Park : IEqualityComparer<Park>
{
public double ticketPrice { get; set; }
public string address { get; set; }
public int peoples { get; set; } public bool Equals(Park x, Park y) //比较x和y对象是否相同,按照地址比较
{
return x.address == y.address;
} public int GetHashCode(Park obj)
{
return obj.ToString().GetHashCode();
}
}
}
或者将比较器单独写成一个类也可以,更多详细信息参见以下链接:
http://msdn.microsoft.com/zh-cn/library/ms132151.aspx
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
try
{ BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary<Box, String> boxes = new Dictionary<Box,
string>(boxEqC); Box redBox = new Box(4, 3, 4);
Box blueBox = new Box(4, 3, 4); boxes.Add(redBox, "red");
boxes.Add(blueBox, "blue"); Console.WriteLine(redBox.GetHashCode());
Console.WriteLine(blueBox.GetHashCode());
}
catch (ArgumentException argEx)
{ Console.WriteLine(argEx.Message);
}
}
} public class Box
{
public Box(int h, int l, int w)
{
this.Height = h;
this.Length = l;
this.Width = w;
}
public int Height { get; set; }
public int Length { get; set; }
public int Width { get; set; }
} class BoxEqualityComparer : IEqualityComparer<Box>
{ public bool Equals(Box b1, Box b2)
{
if (b1.Height == b2.Height & b1.Length == b2.Length
& b1.Width == b2.Width)
{
return true;
}
else
{
return false;
}
} public int GetHashCode(Box bx)
{
int hCode = bx.Height ^ bx.Length ^ bx.Width;
return hCode.GetHashCode();
} }
IEqualityComparer的使用的更多相关文章
- 快速创建 IEqualityComparer 实例:改进
两年前,我写了篇文章<快速创建 IEqualityComparer<T> 和 IComparer<T> 的实例>,文中给出了一个用于快速创建 IEqualityCo ...
- IEqualityComparer<T>
在linq中使用union和distinct都不起作用,结果发现必须传入一个实现了IEqualityComparer<T>的比较器 public class CompareUser : I ...
- Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct
1. DistinctEqualityComparer.cs public class DistinctEqualityComparer<T, V> : IEqualityComparer ...
- 用泛型的IEqualityComparer<T>接口去重复项
提供者:porschev 题目:下列数据放在一个List中,当ID和Name都相同时,去掉重复数据 ID Name 1 张三 1 李三 1 小伟 1 李三 2 李四 2 李武 ----- ...
- IEqualityComparer 去重
1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...
- IEqualityComparer<T>接口
IEqualityComparer<T>接口的对象的主要作用在于自定义判断两个对象是否相等. 其中最常用的方法: bool Equals(T x, T y); 实现该方法用于比较两个对象是 ...
- 于快速创建 IEqualityComparer<T> 实例的类 Equality<T>
于快速创建 IEqualityComparer<T> 实例的类 Equality<T> 原文中的 Equality<T> 实现如下: 1 2 3 4 5 6 7 8 ...
- c# 利用IEqualityComparer接口去除DataTable重复数据
IEqualityComparer主要适用于定义方法以支持对象的相等比较.可以实现集合的自定义相等比较.即,您可以创建自己的相等定义,并指定此定义与接受 IEqualityComparer 接口的集合 ...
- C# IEqualityComparer类型参数写法
最近在使用Union.Except时,由于默认的对比不太好使,所以需要自定义对比器,下面附上代码. class MaterialListComparer : IEqualityComparer< ...
- C# IEqualityComparer 去重
1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...
随机推荐
- Linux下分析某个进程CPU占用率高的原因
Linux下分析某个进程CPU占用率高的原因 通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用 1.top 查到占用cpu高的进程pid 2.查看该pid的线程 ...
- 安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法
安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法 原创 2015年07月31日 ...
- Python web框架——Tornado
Tornado是一个Python Web框架和异步网络库,最初由FriendFeed开发.通过使用非阻塞网络I / O,Tornado可以扩展到数万个开放连接,使其成为需要长时间连接每个用户的长轮询, ...
- oreilly 用户故事地图
这本书是完全买亏了,一点作用也没有. 整篇有用的字很少,还花了我¥16,总结如下: 用户故事模板: 作为用户角色(who),我想要某项功能(what),这样我可以 XXX(原因,why)
- ffmpeg默认输出中文为 UTF-8
在使用ffmpeg 进行对音视频文件解码输出信息的时候会出现乱码. 从网上找到了说ffmpeg默认格式 为 utf-8 如果vs工程使用的的 Unicode 则需要将 utf-8转 Unicode 才 ...
- U3D 场景切换时 脚本对象,GO对象,资源对象的问题
脚本对象:在LUA中写了一个actor类,它封装了角色逻辑,引用了一个GO. GO对象:通过 GameObject.Instantiate(资源对象)生成的. 资源对象:制作好的模型prefab. 在 ...
- ConcurrentDictionary内部机制粗解
ConcurrentDictionary是线程安全类,是什么在保证? 内部类 private class Tables { internal readonly Node[] m_buckets; // ...
- intellij idea运行Android程序时报错;Unable to locate adb within SDK
环境:intellij idea15 问题:运行Android时报错Throwable:Unable to locate adb within SDK 解决方法:在SDK安装目录的\platfor ...
- Python help() 函数
Python help() 函数 Python 内置函数 描述 help() 函数用于查看函数或模块用途的详细说明. 语法 help 语法: help([object]) 参数说明: object ...
- MYSQL 存储过程通用
返回随机时间 函数 )) ) CHARSET utf8 BEGIN )); -- 随机天数 60天以内随机天数 )); -- 随机小时 ));-- 随机分 ));-- 随机秒 ); IF type = ...