1 回答
![?](http://img1.sycdn.imooc.com/5458689e000115c602200220-100-100.jpg)
TA贡献1799条经验 获得超8个赞
您需要稍微修饰一下 GroupKeys 对象。由于它是一个引用类型,并且 GroupBy 使用的是Default Equality Comparer,它的部分检查将测试引用相等性,这将始终返回 false。
您可以通过覆盖Equals和GetHashCode方法来调整您的 GroupKeys 类以测试结构是否相等。例如,这是由 ReSharper 生成的:
private class GroupKeys
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public override int GetHashCode()
{
unchecked
{
return ((Key1 != null ? Key1.GetHashCode() : 0) * 397) ^ (Key2 != null ? Key2.GetHashCode() : 0);
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != this.GetType())
return false;
return Equals((GroupKeys)obj);
}
public bool Equals(GroupKeys other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return string.Equals(Key1, other.Key1)
&& string.Equals(Key2, other.Key2);
}
}
- 1 回答
- 0 关注
- 241 浏览
添加回答
举报