LINQ Except"引用类型"用法

值类型的比较Except 直接就比了

引用类型的如下:

var resultExcept = Expert_ItemSource.Except(Invert_ItemSource, new MyCompare()).ToList();
 //比较器
public class MyCompare : IEqualityComparer<ConsultationExpertApiModel>
        {
            public bool Equals(ConsultationExpertApiModel b1, ConsultationExpertApiModel b2)
            {
                if (b1.MEMBER_ID == b2.MEMBER_ID)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public int GetHashCode(ConsultationExpertApiModel bx)
            {
                int hCode = bx.MEMBER_ID.GetHashCode() ^ bx.MEMBER_ID.GetHashCode();
                return hCode.GetHashCode();
            }
        }
List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);
list1.Add(3);
List<int> list2 = new List<int>();
list2.Add(3);
list2.Add(4);
list2.Add(5);
//得到的结果是4,5 即减去了相同的元素。
List<int> list3 = list2.Except(list1).ToList();
foreach (int i in list3)
{
    MessageBox.Show(i.ToString());
}

当我们用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();
        }
    }
}

下面的示例演示如何实现的相等比较器可在 Except<TSource> 方法。

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}


class ProductComparer : IEqualityComparer<Product>
{

    public bool Equals(Product x, Product y)
    {


        if (Object.ReferenceEquals(x, y)) return true;


        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.Code == y.Code && x.Name == y.Name;
    }

    public int GetHashCode(Product product)
    { 
        if (Object.ReferenceEquals(product, null)) return 0;

        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        int hashProductCode = product.Code.GetHashCode();

        return hashProductName ^ hashProductCode;
    }

}

实现此比较器后,您可以使用的序列 Product 中的对象 Except<TSource> 方法,如下面的示例中所示。

Product[] fruits1 = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 },
                        new Product { Name = "lemon", Code = 12 } };

Product[] fruits2 = { new Product { Name = "apple", Code = 9 } };

IEnumerable<Product> except =
    fruits1.Except(fruits2, new ProductComparer());

foreach (var product in except)
    Console.WriteLine(product.Name + " " + product.Code);

/* 
  orange 4
  lemon 12
*/

  1. GetHashCode是判断对象是否相等的快速检查,所以GetHashCode不需要太复杂,因为即使GetHashCode判断相同了,后面还有Equals保证精确对比。(Note:但是尽量保证GetHashCode就能判断出来,太多的hash冲突,也会增加不必要的开销。这就要看GetHashCode重写得有没有水平了)
  2. 理解重写GetHashCode的必要性。

建议题主了解一下哈希和哈希表,就会知道这个哈希码(HashCode)是什么东西了。

简而言之,哈希代码是一个用于在相等测试过程中标识对象的数值。它还可以作为一个集合中的对象的索引。如果两个对象的 Equals 比较结果相等,则每个对象的 GetHashCode 方法都必须返回同一个值。 如果两个对象的比较结果不相等,这两个对象的 GetHashCode 方法不一定返回不同的值。

简而言之,如果你发现两个对象 GetHashCode() 的返回值相等,那么这两个对象就很可能是同一个对象;但如果返回值不相等,这两个对象一定不是同一个对象。


实例

OrderCompare oc= new OrderCompare();
IEnumerable<Model_OrderInfo> DifferenceSequence = orderList1.Except(OrderinfoList2,oc);

if (DifferenceSequence.Any()) 
{
    foreach (Model_OrderInfo item in DifferenceSequence) 
    {
        sBuilder.Append(item.OrderSn + "--" + item.ProdSn + "<br/>");
    }
    new SendMailHelper().SendMail("123@qq.com", "邮件", null, "不一致的数据",
                                            sBuilder.ToString());
} else 
{
    LogHelper.WriteFile("正确!---------" + DateTime.Now);
}
   //比较器
        public class OrderCompare : IEqualityComparer<Model_OrderInfo>
        {
            public bool Equals(Model_OrderInfo o1, Model_OrderInfo o2)
            {

                if (Object.ReferenceEquals(o1, o2))
                {
                    return true;
                }

                if (Object.ReferenceEquals(o1, null) || Object.ReferenceEquals(o2, null))
                {
                    return false;
                }

                bool flag = o1.Name.Equals(o2.Name) && o1.Age.Equals(o2.Age) && o1.Mobile.Equals(o2.Mobile) && o1.Home.Equals(o2.Home) && o1.Gender.Equals(o2.Gender);
                return flag;
            }

            public int GetHashCode(Model_OrderInfo oinfo)
            { 
                if (oinfo == null)
                {
                    return 0;
                }
                else
                {
                    int hashresult = oinfo.ToString().GetHashCode(); 
                    return hashresult;

                } 
            } 
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值