【C#】【数据操作】使用IEqualityComparer求两个同表数据之间的差值

调用 

//获取户籍列表
                List<DataInput_PopulationInfo> hjList = new RisunDAL<DataInput_PopulationInfo>().GetEntityList(" 1=1 " + sql_where).ToList();
                DataInput_PopulationInfo[] hjArray = hjList.ToArray();
                //获取导入列表
                List<DataInput_PopulationInfo> drList = JsonHepler.GetList<DataInput_PopulationInfo>(dt);
                DataInput_PopulationInfo[] drArray = drList.ToArray();
//求差值
                IEnumerable<DataInput_PopulationInfo> except = drArray.Except(hjArray, new DataInput_PopulationInfoComparer());
//用DataTable 接受数据
               DataTable dt = JsonHepler.ConvertToDataSet<DataInput_PopulationInfo>(except.ToList()).Tables[0];

 类

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

namespace RisunFactoryLibrary
{
    /// <summary>
    /// 户籍/常驻人口信息
    /// </summary>
    public class DataInput_PopulationInfo
    {
        public int id { get; set; }
        /// <summary>
        /// 市区
        /// </summary>
        public string CityName { get; set; }
        /// <summary>
        /// 县区
        /// </summary>
        public string AreaName { get; set; }
        /// <summary>
        /// 镇办
        /// </summary>
        public string TowmName { get; set; }
        /// <summary>
        /// 村居委会
        /// </summary>
        public string VillageName { get; set; }

        /// <summary>
        /// 户号
        /// </summary>
        public string HouseId { get; set; }

        /// <summary>
        /// 本户地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get; set; }

        /// <summary>
        /// 公民身份证号码
        /// </summary>
        public string CardId { get; set; }

        /// <summary>
        /// 政治面貌
        /// </summary>
        public string Political { get; set; }
        /// <summary>
        /// 文化程度
        /// </summary>
        public string Education { get; set; }
        /// <summary>
        /// 婚姻情况
        /// </summary>
        public string Marital { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public string BirthDate { get; set; }


        /// <summary>
        /// 与户主关系
        /// </summary>
        public string Relationship { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }

        /// <summary>
        /// 市id
        /// </summary>
        public int cityId { get; set; }

        /// <summary>
        /// 县id
        /// </summary>
        public int areaId { get; set; }

        /// <summary>
        /// 乡id
        /// </summary>
        public int townId { get; set; }

        /// <summary>
        /// 村id
        /// </summary>
        public int villageId { get; set; }

        /// <summary>
        /// 审核进程ID  0为未提交,1为未审核,2为乡镇:审核驳回,村:审核未通过,3为乡镇:已审核
        /// 创建修改后数据初始为未提交(0),村管理员点击提交后无法修改和删除数据,状态流转至未审核(1)
        /// 乡镇审核后状态流转至已审核(3),驳回后状态流转至审核驳回(2),状态为0,2,3时村管理员可以进行修改
        /// </summary>
        public int process { get; set; }
        /// <summary>
        /// 添加时间
        /// </summary>

        public string AddTime { get; set; }
        /// <summary>
        /// 0为户籍,1为常驻
        /// </summary>

        public int type { get; set; }

    }
    /// <summary>
    /// 求差值调用的类
    /// </summary>
    public class DataInput_PopulationInfoComparer : IEqualityComparer<DataInput_PopulationInfo>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(DataInput_PopulationInfo x, DataInput_PopulationInfo 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;

            //Check whether the products' properties are equal.
            //return x.CardId.Trim().Equals(y.CardId.Trim(), StringComparison.OrdinalIgnoreCase) && x.AddTime.Trim().Equals(y.AddTime.Trim());
            //求差值的条件,该类用身份证和数据时间作为条件进行判断
            return x.CardId.Trim().ToUpper() == y.CardId.Trim().ToUpper() && x.AddTime.Trim().Equals(y.AddTime.Trim());
            //return x.CardId == y.CardId;
        }

        // If Equals() returns true for a pair of objects
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(DataInput_PopulationInfo product)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(product, null)) return 0;

            //Get hash code for the Name field if it is not null.
            //求字段的HASHCODE,需注意此处的求值方法需与Equals方法中的保持一致,即若Equals方法中对数据如何处理,此处也做相同处理
            int hashProductCardId = product.CardId == null ? 0: product.CardId.ToUpper().Trim().GetHashCode();

            //Get hash code for the Code field.
            int hashProductAddTime = product.AddTime == null ? 0 : product.AddTime.Trim().GetHashCode();

            //Calculate the hash code for the product.
            return hashProductCardId ^ hashProductAddTime;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值