zoukankan      html  css  js  c++  java
  • Compare the value of entity field.

        public class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Test();
                Console.ReadLine();
            }
    
            public void Test()
            {
                List<People> initPeopleList = new List<People>();
                People people = new People("Richy",25,"Shanghai");
                initPeopleList.Add(people);
                people = new People("Sherry",25,"Shanghai");
                initPeopleList.Add(people);
    
                List<People> newPeopleList = new List<People>();
                people = new People("Kim",25,"Shanghai");
                newPeopleList.Add(people);
                if (CompareEntity(initPeopleList, newPeopleList))
                {
                    Console.WriteLine("The two entity has some field is not equal.");
                }
                else
                {
                    Console.WriteLine("The two entity are the same.");
                }
            }
            public bool CompareEntity(List<People> initPeopleList,List<People> newPeopleList)
            {
                bool DiffFlag = false;
                if (initPeopleList != null && newPeopleList != null)
                {
                    List<PropertyInfo> infoList = typeof(People).GetProperties().ToList();
                    foreach (var initPeople in initPeopleList)
                    {
                        var commonList = newPeopleList.Where(s => (s.Id == initPeople.Id)).FirstOrDefault(); //We assume id is the primary key.
                        if (commonList != null)
                        {
                            foreach (var propertyInfo in infoList)
                            {
                                var initValue = initPeople.GetType().GetProperty(propertyInfo.Name).GetValue(initPeople, null);
                                var newValue = commonList.GetType().GetProperty(propertyInfo.Name).GetValue(commonList, null);
                                if (initValue != null && !initValue.Equals(newValue))
                                {
                                    DiffFlag = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            DiffFlag = true;
                            break;
                        }
                    }
                }
                else if((initPeopleList == null || newPeopleList == null) && (initPeopleList!=newPeopleList))
                {
                    DiffFlag=true;
                }
    
                return DiffFlag;
            }
    
        }
    
        public class People
        {
            public string EnglishName { get; set; }
            public int Id { get; set; }
            public string Address { get; set; }
    
            public People(string name, int Id, string address)
            {
                this.EnglishName = name;
                this.Id = Id;
                this.Address = address;
            }
        }
    

      

  • 相关阅读:
    浙大版《C语言程序设计(第3版)》题目集 --总结
    | C语言I作业09
    c语言课本及pta作业中运用到的程序思维
    | C语言I作业08
    团队作业(四):描述设计
    实验三《敏捷开发与XP实践》_实验报告
    MyOD(课下作业,选做)
    实验二《面向对象程序设计》_实验报告
    20175226 2018-2019-2《java程序设计》结对编程-四则运算(第二周-阶段总结)
    20175226 类定义
  • 原文地址:https://www.cnblogs.com/bg57/p/3444553.html
Copyright © 2011-2022 走看看