IEqualityComparer的使用

当我们用Linq操作我们自定义的对像数组时,我们会发现有些方法直接使用的话根本不起作用,比如:Distinct、Except、Intersect等扩展方法。

对于我们自定义的对象的比较,我们必须实现IEqualityComparer接口来判断两个对象的相等性。

示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lambda
{
    class Program
    {
        static void Main(string[] args)
        {
            Park p1 = new Park { ticketPrice = 55, address = "南京", peoples = 85 };
            Park p2 = new Park { ticketPrice = 85, address = "北京", peoples = 75 };
            Park p3 = new Park { ticketPrice = 78, address = "多伦多", peoples = 100 };
            List<Park> parks = new List<Park>(){
                new Park { ticketPrice = 11, address = "天堂", peoples = 1000 },
                new Park { ticketPrice = 11, address = "天堂", peoples = 1000 }
            };
            parks.Add(p1);
            parks.Add(p2);
            parks.Add(p3);

            var diff = from c in parks.Distinct(new Park())

                       select c;
            foreach (var item in diff)
            {
                Console.WriteLine(item.address);
            }
        }
        
    }

    class Park : IEqualityComparer<Park>
    {
        public double ticketPrice { get; set; }
        public string address { get; set; }
        public int peoples { get; set; }

        public bool Equals(Park x, Park y)    //比较x和y对象是否相同,按照地址比较
        {
            return x.address == y.address;
        }

        public int GetHashCode(Park obj)  
        {
            return obj.ToString().GetHashCode();
        }
    }
}

  

或者将比较器单独写成一个类也可以,更多详细信息参见以下链接:

http://msdn.microsoft.com/zh-cn/library/ms132151.aspx

using System;
using System.Collections.Generic;
class Example
{
    static void Main()
    {
        try
        {

            BoxEqualityComparer boxEqC = new BoxEqualityComparer();

            Dictionary<Box, String> boxes = new Dictionary<Box,
                                                string>(boxEqC);

            Box redBox = new Box(4, 3, 4);
            Box blueBox = new Box(4, 3, 4);

            boxes.Add(redBox, "red");
            boxes.Add(blueBox, "blue");

            Console.WriteLine(redBox.GetHashCode());
            Console.WriteLine(blueBox.GetHashCode());
        }
        catch (ArgumentException argEx)
        {

            Console.WriteLine(argEx.Message);
        }
    }
}

public class Box
{
    public Box(int h,  int l, int w)
    {
        this.Height = h;
        this.Length = l;
        this.Width = w;
    }
    public int Height { get; set; }
    public int Length { get; set; }
    public int Width { get; set; }
}


class BoxEqualityComparer : IEqualityComparer<Box>
{

    public bool Equals(Box b1, Box b2)
    {
        if (b1.Height == b2.Height & b1.Length == b2.Length
                            & b1.Width == b2.Width)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public int GetHashCode(Box bx)
    {
        int hCode = bx.Height ^ bx.Length ^ bx.Width;
        return hCode.GetHashCode();
    }

}

  

原文地址:https://www.cnblogs.com/hyshareex/p/4076126.html