[翻译]如何使用LINQ方法来比较自定义类型的对象
来源:互联网 发布:金十数据软件下载 编辑:程序博客网 时间:2024/06/06 20:11
How to use LINQ methods to compare objects of custom types http://blogs.msdn.com/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx
在操作对象集合上,LINQ提供了一个很方便的语法和很多有用的方法。然而,要使对象被LINQ的比较方法(比如Distinct or Intersect)正确的处理,对象类型必须满足特定的要求。
让我们看看Distinct方法,distinct方法返回一个集合里面所有不同的对象。
List<int> numbers = new List<int> { 1, 1, 2, 3 };
var distinctNumbers = numbers.Distinct();
foreach (var number in distinctNumbers)
Console.WriteLine(number);
The output is:
1
2
3
如果要在自定义类型的对象集合中使用Distinct方法,我们该如何做?比如下面的例子:
class Number
{
public int Digital { get; set; }
public String Textual { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Number> numbers = new List<Number> {
new Number { Digital = 1, Textual = "one" },
new Number { Digital = 1, Textual = "one" } ,
new Number { Digital = 2, Textual = "two" } ,
new Number { Digital = 3, Textual = "three" } ,
};
var distinctNumbers = numbers.Distinct();
foreach (var number in distinctNumbers)
Console.WriteLine(number.Digital);
}
}
代码顺利通过编译,但是结果却不是我们期望的:
1
1
2
3
怎么会这样呢,答案就隐藏在LINQ的实现细节中。一个类型要想用Distinct方法,必须实现IEquatable<T> 接口并实现Equals和GetHashCode方法。
所以,之前那个例子中的Number class应该如下实现:
class Number: IEquatable<Number>
{
public int Digital { get; set; }
public String Textual { get; set; }
public bool Equals(Number other)
{
// Check whether the compared object is null.
if (Object.ReferenceEquals(other, null)) return false;
// Check whether the compared object references the same data.
if (Object.ReferenceEquals(this, other)) return true;
// Check whether the objects’ properties are equal.
return Digital.Equals(other.Digital) &&
Textual.Equals(other.Textual);
}
// If Equals returns true for a pair of objects,
// GetHashCode must return the same value for these objects.
public override int GetHashCode()
{
// Get the hash code for the Textual field if it is not null.
int hashTextual = Textual == null ? 0 : Textual.GetHashCode();
// Get the hash code for the Digital field.
int hashDigital = Digital.GetHashCode();
// Calculate the hash code for the object.
return hashDigital ^ hashTextual;
}
}
但是如果你的类型不能被修改,比如是在类库中(dll),这样你就不能让这个类去实现IEquatable<T>接口了。答案就是新建自己的 equality comparer类,并将comparer类的实例作为Distinct方法的参数传进去。
Equality comparer必须实现IEqualityComparer<T>接口,并提供GetHashCode和Equals方法的实现。
以下为Number类的 Equality comparer 类:
class NumberComparer : IEqualityComparer<Number>
{
public bool Equals(Number x, Number y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) ||
Object.ReferenceEquals(y, null))
return false;
return x.Digital == y.Digital && x.Textual == y.Textual;
}
public int GetHashCode(Number number)
{
if (Object.ReferenceEquals(number, null)) return 0;
int hashTextual = number.Textual == null
? 0 : number.Textual.GetHashCode();
int hashDigital = number.Digital.GetHashCode();
return hashTextual ^ hashDigital;
}
}
不要忘记把 自定义的comparer类的实例作为Distinct的输入参数:
var distinctNumbers = numbers.Distinct(new NumberComparer());
当然,这个规则除了适用于Distinct方法,还适用于Constains,Except,Intersect和Union方法。通常地,如果你看到LINQ方法中有一个重载方法接受IEqualityComparer<T>为参数,那就说明你自定义的类型需要实现IEquatable<T> 或者创建自己的equality comparer.
其他资料(原文无此部分):
http://msdn.microsoft.com/en-us/library/ms131190.aspx
http://blogs.msdn.com/jaredpar/archive/2009/01/15/if-you-implement-iequatable-t-you-still-must-override-object-s-equals-and-gethashcode.aspx
- [翻译]如何使用LINQ方法来比较自定义类型的对象
- 怎么使用LINQ方法来比较自定义类型对象
- 【翻译】使用LINQ来简化编程的7个技巧
- java比较自定义对象相等的方法
- 使用反射+泛型方法来处理Linq的修改
- 如何使用LINQ来简化编程
- 使用运行是类型标识来确定对象的类型
- 如何使用软件的方法来查找一个QML的类型的所有的property
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- TreeSet集合存放自定义类型的对象,可以使用比较器,实现Comparable接口自己设置排序方式
- 关于自定义比较函数 usort 如何使用 类中的方法
- TypeUtil 提供方法来判断对象或类的类型
- LINQ:使用自定义比较器过滤序列中重复的元素
- EASYUI 使用自定义的校验类型来联动校验表单
- 面向对象方法和结构化方法比较,形式化方法的实际运用困难,及如何结合使用这三种
- TCPMP播放器UI的修改方法
- 得到bean的GetBeanServlet方法
- 我是只菜鸟。。。
- 堆排序 heapSort
- Powerdesigner 设计主键code不能重复等问题
- [翻译]如何使用LINQ方法来比较自定义类型的对象
- FFMpeg框架代码阅读
- 简述FreeTextBox使用方法
- 指针使用
- 使用debugfs恢复 Ext3文件系统`rm -rf /`误删除丢失的数据
- apache + django部署
- 网页错误代码列表 200~505
- 泛型(一)
- Linux程序参数