在lambda表达式中使用相等比较器

时间:2015-01-17 21:47:38

标签: c# .net

我有一个Venue类和一个Coordinate类,如下所示:

class Venue
{
    string Name;
    Coordinate coordinate;
}

class Coordinate
{
    double latitute;
    double longitude;
}

现在,我希望能够根据坐标选择场地,如下所示:

List<Venue> venues = GetAllVenues();

var myVenue = venues.FirstOrDefault(venue=>venue.coordinate == myCoordinate);

我有一个IEqualityComparer实现,但是lambda表达式没有一个以IEqualityComparer作为参数的重载。

如何在lambda表达式中使用我的相等比较器?

编辑:

我的平等比较器看起来像这样:

class CoordinatesEqualityComparer:IEqualityComparer<Coordinate>
    {
        public bool Equals(Coordinate x, Coordinate y)
        {
            return x.RowIndex == y.RowIndex && x.ColumnIndex == y.ColumnIndex;
        }

        public int GetHashCode(Coordinate obj)
        {
            return obj.GetHashCode();
        }
    }

当我执行Union()操作时,即使两个列表中的坐标相同,它也无法正常工作。

List<Coordinates> coordinates; 
CoordinatesEqualityComparer comparer; 
coordinates.Union(someOtherListOfCoordinates, comparer); 

但是,当我与自己结合时,它就有效。我究竟做错了什么?它与GetHashCode()实现有关吗?

编辑2: 修复GetHashCode()方法似乎可以解决问题。

public int GetHashCode(Coordinates obj)
        {
            // Warning:Hack. Use two prime numbers to generate a hash based on two properties.
            return obj.RowIndex.GetHashCode() * 7 + obj.ColumnIndex.GetHashCode() * 13 ;
        }

2 个答案:

答案 0 :(得分:4)

你试过了吗?

var ec = new YourEqualityComparer();
var myVenue = venues.FirstOrDefault(venue => 
                                         ec.Equals(venue.coordinate, myCoordinate));


<小时/>

当然,另一种方法是为==课程定义Coordinate运算符,然后您不需要IEqualityComparer

class Coordinate
{
    double latitude;
    double longitude;

    public override bool Equals(object obj)
    {
        return Object.ReferenceEquals(this, obj)) ||
               this == (other as Coordinate);
    }

    public static bool operator ==(Coordinate l, Coordinate r)
    {
        return ((object)l == null && (object)r == null) || 
               ((object)l != null && (object)r != null) &&
               // equality check including epsilons, edge cases, etc.
    }

    public static bool operator !=(Coordinate l, Coordinate r)
    {
        return !(l == r);
    }
}

答案 1 :(得分:1)

我会实现IEquatable<Coordinate>,覆盖Equals(object),覆盖GetHashCode()和==!=这样的运算符:

public class Coordinate : IEquatable<Coordinate>
{
    public double Latitide { get; set; }
    public double Longitude { get; set; }

    public bool Equals(Coordinate other)
    {
        if (other == null)
        {
            return false;
        }
        else
        {
            return this.Latitide == other.Latitide && this.Longitude == other.Longitude;
        }
    }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Coordinate);
    }

    public override int GetHashCode()
    {
        return this.Latitide.GetHashCode() ^ this.Longitude.GetHashCode();
    }

    public static bool operator ==(Coordinate value1, Coordinate value2)
    {
        if (!Object.ReferenceEquals(value1, null) && Object.ReferenceEquals(value2, null))
        {
            return false;
        }
        else if (Object.ReferenceEquals(value1, null) && !Object.ReferenceEquals(value2, null))
        {
            return false;
        }
        else if (Object.ReferenceEquals(value1, null) && Object.ReferenceEquals(value2, null))
        {
            return true;
        }
        else
        {
            return value1.Latitide == value2.Latitide && value1.Longitude == value2.Longitude;
        }
    }

    public static bool operator !=(Coordinate value1, Coordinate value2)
    {
        return !(value1 == value2);
    }
}