Lets say I have two view models.
View model 1:
public string Name {get;set;} = Harry
public int Age {get;set;} = 19
public string Address {get;set;} = Somewhere
public string PhoneNumber {get;set;} = 1234567899
View model 2:
public string Name {get;set;} = Harry
public int Age {get;set;} = 19
public string Address {get;set;} = Here
public string PhoneNumber {get;set;} = 1234567899
So as you can you see value of the property Address is different. My question is how do we compare these two view models and as you can see Address value is different and I need Address property to be added to list after comparing these two viewmodels.
If you need a list of differences you can take it with: (where Model is your View Model class )
public static List<string> GetDifferences(Model first, Model second) =>
typeof(Model).GetProperties()
.Where(property => property.GetValue(first) != property.GetValue(second))
.Select(property => property.Name)
.ToList();
Same method without LINQ:
public static List<string> GetDifferences(Model first, Model second)
{
var result = new List<string>();
foreach (var property in typeof(Model).GetProperties())
{
if (property.GetValue(first) != property.GetValue(second))
{
result.Add(property.Name);
}
}
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.