I have and Employee class
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Address { get; set; }
public string ContactNo { get; set; }
}
Fill method
private static void FillEmployeeList(ref List<Employee> lt1, ref List<Employee> lt2)
{
lt1 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
new Employee{ID=2,Name="Ravi",Age="24",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
new Employee{ID=3,Name="Lavnya",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
new Employee{ID=4,Name="Rupa",Age="31",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874521256"},
new Employee{ID=5,Name="Divya",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541256387"},
};
lt2 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
new Employee{ID=2,Name="Ravindran",Age="30",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
new Employee{ID=3,Name="Chandru",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
new Employee{ID=4,Name="Rakesh",Age="32",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874021256"},
new Employee{ID=5,Name="Suresh",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541056387"},
new Employee{ID=11,Name="Suryakala",Age="28",Address="No.1,Pillayar koil Street,Chennai",ContactNo="9541204782"},
new Employee{ID=12,Name="Thivya",Age="41",Address="No.42,Ellaiamman koil Street,Chennai",ContactNo="9632140874"},
};
}
Comparing two list,
protected List<Employee> ListCompare(List<Employee> lt1, List<Employee> lt2)
{
FillEmployeeList(ref lt1, ref lt2);
List<Employee> lst = new List<Employee>();
if (lt1.Count > 0 && lt2.Count > 0)
{
var result = (from l1 in lt1
join l2 in lt2
on l1.ID equals l2.ID
orderby l1.ID
select new
{
ID = l1.ID,
Name = (l1.Name == l2.Name) ? "$" : (l2.Name + " (Modified)"),
Age = (l1.Age == l2.Age) ? "$" : (l2.Age + " (Modified)"),
Address = (l1.Address == l2.Address) ? "$" : (l2.Address + " (Modified)"),
ContactNo = (l1.ContactNo == l2.ContactNo) ? "$" : (l2.ContactNo + " (Modified)")
}).ToList();
var result1 = from l1 in lt1
where !(from l2 in lt2
select l2.ID).Contains(l1.ID)
orderby l1.ID
select new
{
ID = l1.ID,
Name = " Deleted",
Age = " Deleted",
Address = " Deleted",
ContactNo = " Deleted"
};
var result2 = from l1 in lt2
where !(from l2 in lt1
select l2.ID).Contains(l1.ID)
orderby l1.ID
select new
{
ID = l1.ID,
Name = l1.Name + " (Added)",
Age = l1.Age + " (Added)",
Address = l1.Address + " (Added)",
ContactNo = l1.ContactNo + " (Added)"
};
var res1 = result.Concat(result1).Concat(result2);
foreach (var item in res1)
{
Employee emp = new Employee();
emp.ID = item.ID;
emp.Name = item.Name;
emp.Age = item.Age;
emp.Address = item.Address;
emp.ContactNo = item.ContactNo;
lst.Add(emp);
}
}
return lst;
}
I want the above ListCompare in generalize to use any king of objects not only for Employee, for other class also(Student, Person etc.). Pls can anyone help me?
Displaying Result,
<pre lang="c#">
List<employee> lt1 = new List<employee>();
List<employee> lt2 = new List<employee>();
List<employee> resultset = new List<employee>();
//string value = "ID";
StringBuilder htmlTable = new StringBuilder();
htmlTable.Append("");
htmlTable.Append("");
resultset = ListCompare(lt1, lt2);
foreach(var item in resultset)
{
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
}
htmlTable.Append("
ID | Name | Age | Address | ContactNo |
---|
" + item.ID + " | " + item.Name + " | " + item.Age + " | " + item.Address + " | " + item.ContactNo + " |
");
PlaceHolder1.Controls.Add(new Literal { Text = htmlTable.ToString() });
Output:
ID Name Age Address ContactNo
---------------------------------------------------------------------------------
1 $ $ $ $
2 Ravindran (Modified) 30 (Modified) $ $
3 Chandru (Modified) 30 (Modified) $ $
4 Rakesh (Modified) 32 (Modified) $ 9874021256(Modified)
5 Suresh (Modified) $ $ 8541056387(Modified)
11 Suryakala (Added) 28 (Added) No.1,Pillayar
koil Street,
Chennai (Added) 9541204782 (Added)
12 Thivya (Added) 41 (Added) No.42,Ellaiamman koil
Street,Chennai(Added)
9632140874 (Added)
What I have tried:
Getting correct output but i want in generalized way to use it for any class of objects like Student, Person etc.