Net Core教程

C#比较实体与实体或集合实体与集合实体的数据比较

本文主要是介绍C#比较实体与实体或集合实体与集合实体的数据比较,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
        /// <summary>
        /// 实体类的新旧值比较方法(参数1 新的实体类,参数二 旧实体类,参数三 需要比较的属性的集合,参数四 需要比较的集合的显示值)
        /// </summary>
        /// <param name="newAverage">新实体类</param>
        /// <param name="oldAverage">旧实体类</param>
        /// <param name="compareAttribute">需要比较的属性集合</param>
        /// <param name="compareAttributeName">需要比较的属性集合的中文名集合</param>
        /// <returns>返回有差异的数据的拼接字符串</returns>
        public static string ModelCompare(object newAverage, object oldAverage, List<string> compareAttribute, List<string> compareAttributeName)
        {
            if (newAverage == null || oldAverage == null)
            {
                return "传入比较值为空,请检查传入值!";
            }
            if (compareAttribute.Count() != compareAttributeName.Count())
            {
                return "传入的要比较的列与对应的显示值数量不等,请检查要比较的列的数量与对应显示列的数量!";
            }
            string differentThing = null;
            List<object> newValue = new List<object>();
            List<object> oldValue = new List<object>();
            //判断是否有修改
            bool isDiffOrNot = false;
            try
            {
                // 只有两个对象都是同一类型的才有可比性
                if (newAverage.GetType() == oldAverage.GetType())
                {
                    foreach (string item in compareAttribute)
                    {
                        //获取新值的要比较项的集合
                        foreach (PropertyInfo pi in newAverage.GetType().GetProperties())
                        {
                            string name = pi.Name.ToString();
                            object value = pi.GetValue(newAverage, null);
                            if (item.Equals(name))
                            {
                                newValue.Add(value);
                                break;
                            }
                        }
                    }
                    foreach (var item in compareAttribute)
                    {
                        //获取旧值的要比较项的集合
                        foreach (PropertyInfo pi in oldAverage.GetType().GetProperties())
                        {
                            string name = pi.Name;
                            object value = pi.GetValue(oldAverage, null);
                            if (item.Equals(name))
                            {
                                oldValue.Add(value);
                                break;
                            }
                        }
                    }
                    //新旧值比较,有不同的就添加到返回值中去
                    for (int i = 0; i < newValue.Count(); i++)
                    {
                        if (string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                        {
                        }
                        else if (!string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                        {
                            isDiffOrNot = true;
                            differentThing += compareAttributeName[i] + ":无数据-->>" + newValue[i] + ";";
                        }
                        else if (string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && !string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                        {
                            isDiffOrNot = true;
                            differentThing += compareAttributeName[i] + ":" + oldValue[i] + "-->>无数据;";
                        }
                        else
                        {
                            if (!newValue[i].Equals(oldValue[i]))
                            {
                                isDiffOrNot = true;
                                differentThing += compareAttributeName[i] + ":" + oldValue[i] + "-->>" + newValue[i] + ";";
                            }
                        }
                    }
                    if (!isDiffOrNot)
                        differentThing = "新旧值无变化!";
                }
                else
                    differentThing = "请输入相同类型进行比较!";
            }
            catch (Exception ex)
            {
                //Common.WriteDayLog("ModelCompare\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + ex.Message + "\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + DateTime.Now);
                throw;
            }
            return differentThing;
        }

        /// <summary>
        /// 集合类的新旧值比较方法(参数1 新的实体类的集合,参数二 旧实体类的集合,参数三 需要比较的属性的集合,参数四 需要比较的集合的显示值)
        /// </summary>
        /// <param name="newAverages">新集合类</param>
        /// <param name="oldAverages">旧集合类</param>
        /// <param name="compareAttribute">需要比较的属性的集合</param>
        /// <param name="compareAttributeName">需要比较的属性集合的中文名集合</param>
        /// <param name="matchAveragesAttribute">用来整理新旧集合的属性名</param>
        /// <returns>返回有差异的数据的拼接字符串的集合</returns>
        public static List<string> LstCompare<T>(List<T> newAverages, List<T> oldAverages, List<string> compareAttribute, List<string> compareAttributeName, string matchAveragesAttribute)
        {
            List<string> differentThings = new List<string>();
            if (newAverages.Count() == 0 || oldAverages.Count() == 0)
            {
                differentThings.Add("传入比较的集合有一个或多个为空,请检查传入值!");
            }
            if (compareAttribute.Count() != compareAttributeName.Count())
            {
                differentThings.Add("传入的要比较的列与对应的显示值数量不等,请检查要比较的列的数量与对应显示列的数量!");
            }
            //数量不等时调用matchAveragesAttribute整理新旧集合数量
            int addAverages = 0;//指定多出来的数据列是添加还是删除(0、不变;1、添加;2、删除)
            List<T> diffAttributeValue = new List<T>();//原集合中多出来的数据列
            if (newAverages.Count() != oldAverages.Count())
            {
                //开始匹配整理集合
                if (newAverages.Count() > oldAverages.Count())
                {
                    addAverages = 1;
                    diffAttributeValue = NotFindAverages(newAverages.ToList(), oldAverages.ToList(), matchAveragesAttribute);
                    foreach (var item in diffAttributeValue)
                    {
                        newAverages.Remove(item);
                    }
                }
                else
                {
                    addAverages = 2;
                    diffAttributeValue = NotFindAverages(oldAverages.ToList(), newAverages.ToList(), matchAveragesAttribute);
                    foreach (var item in diffAttributeValue)
                    {
                        oldAverages.Remove(item);
                    }
                }
            }

            int count = 0;
            int countdiff = 1;

            try
            {
                //集合内包含的数据列一样多才有意义
                if (newAverages.Count() == oldAverages.Count())
                {
                    foreach (var item in newAverages)
                    {
                        string diffT = ModelCompare(item, oldAverages[count], compareAttribute, compareAttributeName);
                        if (diffT.Contains(";"))
                        {
                            diffT = "【第" + countdiff + "条修改项】" + diffT;
                            differentThings.Add(diffT);
                            countdiff++;
                        }
                        count++;
                    }
                }
                string addOrDelete = null;
                if (addAverages == 1)
                {
                    addOrDelete = SplicingAveragesValue(diffAttributeValue, compareAttribute, compareAttributeName);
                    differentThings.Add("【添加】;" + addOrDelete);
                }
                else if (addAverages == 2)
                {
                    addOrDelete = SplicingAveragesValue(diffAttributeValue, compareAttribute, compareAttributeName);
                    differentThings.Add("【删除】;" + addOrDelete);
                }
            }
            catch (Exception ex)
            {
                //Common.WriteDayLog("LstCompare\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + ex.Message + "\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + DateTime.Now);
                throw;
            }
            return differentThings;
        }

        /// <summary>
        /// 根据标志属性筛选出两个集合类中不同的类的集合(参数1 数量较多的集合,参数二 数量较少的集合,参数三 标志属性)
        /// </summary>
        /// <param name="bigAverages">数量较多的集合</param>
        /// <param name="smallAverages">数量较少的集合</param>
        /// <param name="matchAveragesAttribute">标志属性</param>
        /// <returns>返回找出来的类的集合</returns>
        public static List<T> NotFindAverages<T>(List<T> bigAverages, List<T> smallAverages, string matchAveragesAttribute)
        {
            List<T> diffAttributeValue = new List<T>();
            foreach (var item in bigAverages)
            {
                foreach (PropertyInfo pi in item.GetType().GetProperties())
                {
                    string name = pi.Name.ToString();
                    object value = pi.GetValue(item, null);
                    if (matchAveragesAttribute.Equals(name))
                    {
                        bool isNotFind = true;
                        foreach (var item2 in smallAverages)
                        {
                            foreach (PropertyInfo pi2 in item2.GetType().GetProperties())
                            {
                                string name2 = pi2.Name.ToString();
                                object value2 = pi2.GetValue(item2, null);
                                if (matchAveragesAttribute.Equals(name2))
                                {
                                    if (value.ToString() == value2.ToString())
                                    {
                                        isNotFind = false;
                                    }
                                    break;
                                }
                            }
                            if (!isNotFind)
                            {
                                break;
                            }
                        }
                        if (isNotFind)
                        {
                            diffAttributeValue.Add(item);
                        }
                        break;
                    }
                }
            }
            return diffAttributeValue;
        }

        /// <summary>
        /// 拼接多余的实体类与显示属性对应成字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="diffAttributeValue"></param>
        /// <param name="compareAttribute"></param>
        /// <param name="compareAttributeName"></param>
        /// <returns></returns>
        public static string SplicingAveragesValue<T>(List<T> diffAttributeValue, List<string> compareAttribute, List<string> compareAttributeName)
        {
            string addOrDelete = null;
            try
            {
                foreach (var item3 in diffAttributeValue)
                {
                    int count = 0;
                    foreach (string item in compareAttribute)
                    {
                        foreach (PropertyInfo pi in item3.GetType().GetProperties())
                        {
                            string name = pi.Name.ToString();
                            object value = pi.GetValue(item3, null);
                            if (item.Equals(name))
                            {
                                addOrDelete += compareAttributeName[count] + ":" + value + ";";
                                break;
                            }
                        }
                        count++;
                    }
                }
            }
            catch (Exception ex)
            {
                addOrDelete = ex.Message;
                throw;
            }
            return addOrDelete;
        }

  

这篇关于C#比较实体与实体或集合实体与集合实体的数据比较的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!