简体   繁体   中英

Compare two lists or datatable

I have two excel sheets for employees,

one has the current status, one has the old status,

I want to compare them to check which employees have their status changed.

currently i have this:

var result = from x in data.Worksheet<Employee>("Tradesmen")
                         select x;

and this:

var resultNew = from x in dataNew.Worksheet<Employee>("Tradesmen")
                         select x;

where result is a datatable has the old status, and resultNew is a datatable has the current status.

What I tried

I tried change these datatable to lists like this:

masterEmployees = new List<Employee>();
            foreach (var row in result)
            {
                masterEmployees.Add(new Employee(row.Code, row.Name, row.WorkingStatus));
            }

 foreach (var row in resultNew)
            {
                newEmployees.Add(new Employee(row.Code, row.Name, row.WorkingStatus));
            }

where Employee is aa simple class with Code , Name and WorkingStatus field.

My question

I want to know which employees that their status changed so how can I do that in the lists ? of is it better to do that in datatable ?

Try this

    var result = new List<Employee>();
    var resultNew = new List<Employee>();

    var changed = (from i in result
                   join j in resultNew on i.Code equals j.Code
                   where i.WorkingStatus != j.WorkingStatus
                   select new { Name = i.Name, OldWorkingStatus = i.WorkingStatus, NewWorkingStatus = j.WorkingStatus        }).ToList();

Don't forget

using System.Linq;

Update

If you do this i think you'll be ok(the same for the resultNew variable).

var result = (from x in data.Worksheet("Tradesmen") select x) .ToList();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM