当我们用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. web开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  2. [Python] numpy.sum

    import numpy as np #Syntax: numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<class numpy._ ...

  3. Rhythmk 一步一步学 JAVA (22) JAVA 网络编程

    1.获取主机信息 @Test public void GetDomainInfo() throws UnknownHostException { String domain = "www.b ...

  4. IOS上架(九) AppStore编译生成ipa文件并上传

    IOS上架上传ipa文件 AppStore https://itunesconnect.apple.com delphi project>option里的CFBundleVersion 上传的版 ...

  5. GridEh排序

    添加pas文件 EhLibADO.pas EhLibCDS.pas EhLibFireDAC.pas 支持FireDAC 设置GridEh的属性 DBGridEh1->SortLocal = t ...

  6. 当前触发事件的两种方式(onclick) 和 ('id') 获取

    1. <input type='text' onclick = 'Clickon(this)'> <script> function Clickon(num){ num.sty ...

  7. 9 MySQL--多表查询

    多表查询: http://www.cnblogs.com/linhaifeng/articles/7267596.html 1.多表连接查询 2.符合条件连接查询 3.子查询 一.准备表 #建表 cr ...

  8. zookeeper 节点讲解以及实际项目运用

    转自:https://www.jianshu.com/p/86acf1df6cdd 前言:最近工作不是很忙,本应该乘着闲暇的时间看书的,之前每天晚上都要翻翻的,可是自己竟然迷恋上了王晓磊 写的 卑鄙的 ...

  9. python中os.system()的返回值

    [python中os.system()的返回值] 如果第三方程序返回的是布尔型返回值,os.system会将true转为1,false转为0进行返回. 问题: /bin/xxx.py是一个返回码为1的 ...

  10. codeforces:818G Four Melodies分析

    题目 题目大意是有一组自然数v1,...,vn,要求在其中找到四个非空子序列(从原来的自然数序列中挑选一部分数,并按原先后关系排序),这些子序列互不相交,且每个子序列中的前后元素的值要么差值的绝对值为 ...