这就是我想要做的。我正在使用 LINQ to XML 查询一个 XML 文件,它为我提供了一个 IEnumerable <T
> 对象,其中 T 是我的“村庄”类,其中填充了此查询的结果。有些结果是重复的,所以我想对 IEnumerable 对象执行 Distinct(),如下所示:
public IEnumerable<Village> GetAllAlliances()
{
try
{
IEnumerable<Village> alliances =
from alliance in xmlDoc.Elements("Village")
where alliance.Element("AllianceName").Value != String.Empty
orderby alliance.Element("AllianceName").Value
select new Village
{
AllianceName = alliance.Element("AllianceName").Value
};
// TODO: make it work...
return alliances.Distinct(new AllianceComparer());
}
catch (Exception ex)
{
throw new Exception("GetAllAlliances", ex);
}
}
由于默认比较器不适用于 Village 对象,我实现了一个自定义比较器,如 AllianceComparer 类中所示:
public class AllianceComparer : IEqualityComparer<Village>
{
#region IEqualityComparer<Village> Members
bool IEqualityComparer<Village>.Equals(Village x, Village y)
{
// Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y))
return true;
// Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.AllianceName == y.AllianceName;
}
int IEqualityComparer<Village>.GetHashCode(Village obj)
{
return obj.GetHashCode();
}
#endregion
}
Distinct() 方法不起作用,因为无论有没有它,我都有完全相同数量的结果。另一件事,我不知道这通常是否可行,但我无法进入 AllianceComparer.Equals() 看看可能是什么问题。
我在互联网上找到了这样的例子,但我似乎无法让我的实现工作。
希望这里的人可能会看到这里可能出了什么问题!提前致谢!