Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have tow lists have the same properties and i want to compare the data between old and new DB

C#
List<srsEmployee> srsEmps = db.srsEmployees.ToList(); //from old DB
    List<destEmployee> destEmps = db2.destEmployees.ToList(); //from new DBs


i need Three list shown in the view
1.Newly Added Employee
2.Deleted Employee
3.Common Employee
so anyone can help me ?

this query what i wrote but the result isn't right

What I have tried:

    var Common = destEmps.Where(n => srsEmps.Any(o => o.EmpCode == n.EmpCode)).ToList();
    var Deleted = srsEmps.Where(o => !destEmps.Any(n => n.EmpCode == o.EmpCode)).ToList();
    var NewlyAdded = destEmps.Where(n => !srsEmps.Any(o => o.EmpCode == n.EmpCode)).ToList();
<pre>
Posted
Updated 16-Oct-18 7:19am
Comments
Richard Deeming 16-Oct-18 12:38pm    
What does "isn't right" mean?

Remember, we can't see your screen, access your database, or read your mind. All we have to work with is what you tell us. So help us to help you by telling us what we need to know. :)

1 solution

I do not profess to being adept with LINQ, but did give it a shot with this Proof-of-Concept and it works as I would intend it to.

It is not a 100% solution for you as is, but should be a good base to work with.

C#
namespace ProofOfConcept {
	public class Employee {
		public int EmpCode { get; set; }
		public string EmpName { get; set; }

		public Employee() { }
		public Employee(int Code, string Name) {
			EmpCode = Code;
			EmpName = Name;
		}
	}

	class Program {
		static void Main(string[] args) {

			List<Employee> srsEmps = new List<Employee>();
				srsEmps.Add(new Employee(1, "Me"));
				srsEmps.Add(new Employee(2, "You"));

			List<Employee> destEmps = new List<Employee>();
				destEmps.Add(new Employee(2, "You"));
				destEmps.Add(new Employee(3, "Someone Else"));

			var Deleted =
				from s in srsEmps
				where !(from d in destEmps select d.EmpCode).Contains(s.EmpCode)
				select s;

			var Common =
				from s in srsEmps
				where (from d in destEmps select d.EmpCode).Contains(s.EmpCode)
				select s;

			var NewlyAdded =
				from d in destEmps
				where !(from s in srsEmps select s.EmpCode).Contains(d.EmpCode)
				select d;

			foreach (Employee d in Deleted) {
				Console.WriteLine("Deleted = " + d.EmpName);
			}
			foreach (Employee c in Common) {
				Console.WriteLine("Common = " + c.EmpName);
			}
			foreach (Employee n in NewlyAdded) {
				Console.WriteLine("New = " + n.EmpName);
			}

			Console.WriteLine("Press any key to continue");
			Console.ReadLine();
		}
	}
}
 
Share this answer
 
Comments
Member 13168476 16-Oct-18 14:11pm    
it work ! Thank you
so if i have in my controller
ViewBag.common = Common;
ViewBag.deleted = Deleted;
ViewBag.newlyAdded = NewlyAdded;

and in my view show the result in table like this
@Html.DisplayNameFor(model => model.Name)
@foreach (var item in ViewBag.common)
{
}
@Html.DisplayNameFor(model => model.Name)

@Html.DisplayFor(modelItem =>item)


How i can show the employee name only not all his data? because item dosen't have any property to select
MadMyche 16-Oct-18 14:41pm    
You may need to do a cast/convert to have the correct object set and then you could just use @Html.DisplayFor(model => model.Name).
I myself would probably create an "Employee Comparison Model" which would have the 3 separate lists as properties, and pass that model to the view. Makes it easier
Member 13168476 16-Oct-18 14:28pm    
sorry but if i want to know if any employee is updated and which fields is different
how i write the query ?
var updated = ....
MadMyche 16-Oct-18 14:42pm    
Not knowing what the fields are in the database and class, wouldn't be able to help you with finding updated info
Member 13168476 16-Oct-18 16:00pm    
public class srsEmployee
{
public int Id { get; set; }
public string Name { get; set; }
public string EmpCode { get; set; }
public Nullable<decimal> Salary { get; set; }
public Nullable<system.datetime> StartDate { get; set; }
public Nullable<system.datetime> BOD { get; set; }
public int DepartmentId { get; set; }
public bool Active { get; set; }

public virtual srsDepartment srsDepartment { get; set; }
}


public class destEmployee
{
public int Id { get; set; }
public string Name { get; set; }
public string EmpCode { get; set; }
public Nullable<decimal> Salary { get; set; }
public Nullable<system.datetime> StartDate { get; set; }
public Nullable<system.datetime> BOD { get; set; }
public int DepartmentId { get; set; }
public bool Active { get; set; }

public virtual destDepartment destDepartment { get; set; }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900