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 ...
随机推荐
- 几种查询方法(lambda Linq Enumerable静态类方式)
1.需要一个数据源类: using System; using System.Collections.Generic; namespace Linq { public class Student { ...
- come on!
团队选题与评审(团队作业 2) 队名 Rookie 团队成员的姓名与学号 211606377 覃一霸(队长) 211606346 张江波 211606371 刘治江 211606384 夏培华 211 ...
- XE: Changing the default http port
Oracle XE uses the embedded http listener that comes with the XML DB (XDB) to serve http requests. T ...
- Best free online svn repositories
Maybe you want to develop in a custom team environment or you usualy work on different machines (tha ...
- nodepad++快捷键
在用notepad++进行代码编辑的过程中,其实notepad++也可以进行单行.多行.区块注释和取消注释的....... 快捷键如下: 单行.多行注释 //方式 ...
- spring拾遗(一)——@Value注入static属性
一般情况的下的@Value是用在非静态方法上的,如下: import org.springframework.beans.factory.annotation.Value; import org.sp ...
- 使用java代码编辑oracle数据库
package com.hanqi; import java.io.IOException; import java.sql.Connection; import java.sql.DriverMan ...
- 判断单选框选中不成功,$("#xx").attr("checked")undefined
判断单选框选中状态,各种都不行,受到https://www.cnblogs.com/yxwkf/p/4853014.html 的启发,相关引用: 原来.在jquery1.6版本号便对此做出了改动: [ ...
- Associate File Type with Qt In Mac Os and Win
Win Registry Question One day, my boss want me to finish one function which let the users can double ...
- Get The Client Info From PHP SERVER Arrary
Get The Client Info From PHP SERVER Arrary <?php date_default_timezone_set( "Asiz/Shanghai&q ...