I am using entityframework. I had two records which are retrieved from table using 'id' which is a primary key. Now i want to compare these two table records data and display the old value and new value in my view. Now my question is how to compare two records... There are almost 20 properties in my table from which i retrieve the data. We have to compare each and every property or is there are any best method... Can any one please help me to find the solution..
-
Can you show some code how you are retrieving the data?TRR– TRR2012-05-10 07:37:24 +00:00Commented May 10, 2012 at 7:37
-
See stackoverflow.com/questions/986572/…Eranga– Eranga2012-05-10 07:38:07 +00:00Commented May 10, 2012 at 7:38
-
using (Model.SlmgDataContext dbContext = new Model.SlmgDataContext()) { var student= dbContext.Students.FirstOrDefault(con => con.id== studentid); var previousStudent = dbContext.Students.FirstOrDefault(con => con.id== previousstudentid); } Now i want to compare these two students marks..Swetha Bindu– Swetha Bindu2012-05-10 14:22:43 +00:00Commented May 10, 2012 at 14:22
Add a comment
|
1 Answer
public bool Equals<T>(T first, T second)
{
var f = new List<T>() {first};
var s = new List<T>() {second};
PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (f.Select(x => propertyInfo.Name).FirstOrDefault() != s.Select(x => propertyInfo.Name).FirstOrDefault())
return false;
}
return true;
}
Changed to Equals < T > (T first, T second) as Kim R recommended
Try it :) I haven't tested it