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 ...
随机推荐
- jemalloc内存分配器详解
前言 C 中动态内存分配malloc 函数的背后实现有诸派:dlmalloc 之于 bionic:ptmalloc 之于 glibc:allocation zones 之于 mac os x/ios: ...
- Memcpy, blockcopy的进一步理解
using System; using System.Runtime.InteropServices; using System.IO; namespace tx { struct ST { publ ...
- 迷你MVVM框架 avalonjs 1.3.5发布
本版本主要是修复内存泄漏问题,让其在移动端更好的运作. 修正visible BUG 详见这里 修正$fire方法里的正则错误 详见这里 修正ms-attr BUG,在IE9-11,直接用element ...
- css常用属性总结:关于word-spacing和letter-spacing的使用
前端时间项目版本迭代,修改代码时发现使用了关于word-spacing和letter-spacing.先说下使用场景,以前的项目中,经常遇到某些字符间有一些间距,我看了一些同事的代码是这么实现的: & ...
- linux shell脚本编程笔记(五): 重定向
I/O重定向 简述: 默认情况下始终有3个"文件"处于打开状态, stdin (键盘), stdout (屏幕), and stderr (错误消息输出到屏幕上). 这3个文件和其 ...
- Cocoa Touch(五):网络请求 NSURLSession/AFNetworking, GCD, NSURLResquest
NSURLRequest 网络请求的关键的就是NSURLRequest类,它的实例表示了请求报文实体以及请求的缓存策略等等,各种网络框架的最终目标都是把这个对象编译成为请求报文发送出去.下面用一个实例 ...
- Qt Image Water Marker
QString str = "input.jpg"; if(!img.load(str)){ return; } QImage mark(img.width()/2,img.hei ...
- Create Empty Project In Vs But Not Debug?
问题描述 在使用VS创建一个空的项目管理,然后,添加项目.可是,这个时候,项目虽然可以运行,但是不能Debug进行调试. 解决方法 按照下面三张图像的设置即可.
- Linux之RPM GPG签名
原文地址:http://linux.chinaunix.net/techdoc/system/2007/09/26/968723.shtml GPG在Linux上的应用主要是实现官方发布的包的签名机制 ...
- java 主类的main方法调用其他方法
方法1:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生,如下: public class test{ class A{ void fA(){ S ...