常建57

路漫漫其修远兮。。。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
    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;
        }
    }

  

posted on 2013-11-26 23:06  常建57  阅读(125)  评论(0编辑  收藏  举报