Linq Distinct方法的参数扩展
来源:互联网 发布:系统盘垃圾清理软件 编辑:程序博客网 时间:2024/05/11 01:05
public class PropertyComparer<T> : IEqualityComparer<T>
{private PropertyInfo _PropertyInfo;
/// <summary>
/// Creates a new instance of PropertyComparer.
/// </summary>
/// <param name="propertyName">The name of the property on type T
public PropertyComparer(string propertyName)
{
_PropertyInfo = typeof(T).GetProperty(propertyName,
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
if (_PropertyInfo == null)
{
throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
}
}
public bool Equals(T x, T y)
{
//get the current value of the comparison property of x and of y
object xValue = _PropertyInfo.GetValue(x, null);
object yValue = _PropertyInfo.GetValue(y, null);
//if the xValue is null then we consider them equal if and only if yValue is null
if (xValue == null)
return yValue == null;
//use the default comparer for whatever type the comparison property is.
return xValue.Equals(yValue);
}
public int GetHashCode(T obj)
{
//get the value of the comparison property out of obj
object propertyValue = _PropertyInfo.GetValue(obj, null);
if (propertyValue == null)
return 0;
else
return propertyValue.GetHashCode();
}
}
0 0
- Linq Distinct方法的参数扩展
- Linq Distinct方法的扩展
- 关于对Linq的Distinct的扩展
- linq Distinct的一个简单实现方法
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- 如何很好的使用Linq的Distinct方法
- 为LINQ实现自定义类的Distinct方法
- Linq 中 Distinct 的运用
- C# Linq Distinct的使用
- LINQ vs 扩展方法
- 关于LINQ扩展方法
- LINQ与扩展方法
- linq 扩展方法
- Linq常用扩展方法
- linq扩展方法
- Linq之扩展方法
- linux配置jdk环境变量
- spring+hibernate整合详细步骤解析
- 收集整理网上看到有意思的代码片段
- Visual Studio 2010配置Opencv2.4.9
- Base64编码
- Linq Distinct方法的参数扩展
- Matlab学习记录(函数)
- android:layout_centerVertical="true" --将本控件置于父控件垂直方向的中心位置
- java中JVM的原理
- java之内部类
- Tab 对齐方式
- 通过API远程操作HBase
- BLE多连接
- Linq Distinct方法的扩展