Compare the value of entity field.
2024-10-10 01:22:03
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#编程总结(二)多线程基础
- go语言 类型:数组
- 北大poj-1062
- Pattern Lab - 构建先进的原子设计系统
- https://github.com/oneuijs/You-Dont-Need-jQuery
- Openjudge计算概论-单词翻转
- TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE
- WPF 中,如何使用自定义的resources
- ria service 单元测试
- Java 中 Vector、ArrayList、List 使用深入剖析【转载】
- URAL 1963 Kite 四边形求对称轴数
- Block 实现 浅析
- python 使用 &#39;python -m pip install --upgrade pip&#39;提示PermissionError: [WinError 5] 拒绝访问
- [[NSBundle mainBundle] pathForResource:fileName ofType:]获取文件路径不成功
- 【BZOJ1040】【ZJOI2008】骑士
- [转]C# 4.7.2 安装
- halcon 动态阈值分割之偏移值
- 2018-2019-2 网络对抗技术 20165212 Exp4 恶意代码分析
- .NET Core容器化之多容器应用部署-使用Docker-Compose
- mysql 设置默认时间为now()
热门文章
- mysql 简单游标
- Python中的isinstance函数
- Visual Studio 2013使用SASS和Compass--SASS和Compass安装
- 转:批处理for命令详解
- 利用 Android Studio 和 Gradle 打包多版本APK
- HDU 5724 Chess(博弈论)
- 1.2UISwitch 1.3 自定义UIswitch 1.4pickerView
- 揭秘Amazon反应速度超快的下拉菜单
- JavaWeb学习过程 之c3p0的使用
- Problem C Andy&#39;s First Dictionary(set的使用)