简体   繁体   English

比较两个列表或数据表

[英]Compare two lists or datatable

I have two excel sheets for employees, 我为员工准备了两张Excel工作表,

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. 其中result是数据表具有旧状态,而resultNew是数据表具有当前状态。

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. 其中, Employee是一个具有CodeNameWorkingStatus字段的简单类。

My question 我的问题

I want to know which employees that their status changed so how can I do that in the lists ? 我想知道他们的状态发生了变化的员工,我该如何在lists做到这一点? of is it better to do that in datatable ? 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). 如果您这样做,我认为您会没事的(resultNew变量也一样)。

var result = (from x in data.Worksheet("Tradesmen") select x) .ToList(); var result =(从data.Worksheet(“ Tradesmen”)中的x选择x) .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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