当我们用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的使用的更多相关文章

  1. 快速创建 IEqualityComparer 实例:改进

    两年前,我写了篇文章<快速创建 IEqualityComparer<T> 和 IComparer<T> 的实例>,文中给出了一个用于快速创建 IEqualityCo ...

  2. IEqualityComparer<T>

    在linq中使用union和distinct都不起作用,结果发现必须传入一个实现了IEqualityComparer<T>的比较器 public class CompareUser : I ...

  3. Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct

    1. DistinctEqualityComparer.cs public class DistinctEqualityComparer<T, V> : IEqualityComparer ...

  4. 用泛型的IEqualityComparer<T>接口去重复项

    提供者:porschev 题目:下列数据放在一个List中,当ID和Name都相同时,去掉重复数据 ID Name 1  张三 1  李三 1  小伟 1  李三  2  李四 2  李武 ----- ...

  5. IEqualityComparer 去重

    1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...

  6. IEqualityComparer<T>接口

    IEqualityComparer<T>接口的对象的主要作用在于自定义判断两个对象是否相等. 其中最常用的方法: bool Equals(T x, T y); 实现该方法用于比较两个对象是 ...

  7. 于快速创建 IEqualityComparer<T> 实例的类 Equality<T>

    于快速创建 IEqualityComparer<T> 实例的类 Equality<T> 原文中的 Equality<T> 实现如下: 1 2 3 4 5 6 7 8 ...

  8. c# 利用IEqualityComparer接口去除DataTable重复数据

    IEqualityComparer主要适用于定义方法以支持对象的相等比较.可以实现集合的自定义相等比较.即,您可以创建自己的相等定义,并指定此定义与接受 IEqualityComparer 接口的集合 ...

  9. C# IEqualityComparer类型参数写法

    最近在使用Union.Except时,由于默认的对比不太好使,所以需要自定义对比器,下面附上代码. class MaterialListComparer : IEqualityComparer< ...

  10. C# IEqualityComparer 去重

    1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...

随机推荐

  1. solr搜索之搜索精度问题我已经尽力了!!!

    solr搞了好久了,没啥进展,没啥大的突破,但是我真的尽力了! solr7可能是把默认搜索方式去掉了,如下: 在solr7里找了半天以及各种查资料也没发现这个默认搜索方式,后来想,可能是被edisma ...

  2. nginx.conf_2017-11-24

    user webroot; worker_processes 4; worker_cpu_affinity 1000 0100 0010 0001; worker_rlimit_nofile 6550 ...

  3. PyQt5系列教程(四)信号和槽

    软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 前言 信号(Signal)和槽(Slot)是Qt编程中对象间通讯的机制.在Qt中,每一个QObject对象, ...

  4. Firemonkey Button 颜色

    delphi FMX Firemonkey Button 按钮 颜色 TintColor 颜色 Button1.TintColor:=TAlphaColorRec.Green;

  5. Liunx cannot remove `xxx': Operation not permitted

    链接: http://mangocool.com/detail_1_1439515930283.html 解到原来文件还可以设置隐藏权限,就是这个chattr设置,下面我们来看看这个命令的详解. [r ...

  6. css常用属性总结:关于word-spacing和letter-spacing的使用

    前端时间项目版本迭代,修改代码时发现使用了关于word-spacing和letter-spacing.先说下使用场景,以前的项目中,经常遇到某些字符间有一些间距,我看了一些同事的代码是这么实现的: & ...

  7. ansible Developing Plugins

    Action plugins是模块的前端,可以在调用模块本身之前对控制器执行操作. Cache plugins用于保存“facts”的缓存,以避免代价高昂的fact-gathering操作. Call ...

  8. 74. Search a 2D Matrix (Graph; Divide-and-Conquer)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. Socket调用方式(同步,异步,阻塞,非阻塞)

    同步: 我调用一个功能,该功能没有结束前,我死等结果. 异步: 当一个异步过程调用发出后,调用者不能立刻得到结果.该功能在完成后,通过状态.通知和回调来通知调用者. 同步和非同步关注的是调用者是否等待 ...

  10. Unity5.x Reflection Probe反射探针

    http://blog.csdn.net/yupu56/article/details/53487216