【问题标题】:EFCore Getting Differences between two tablesEFCore 获取两个表之间的差异
【发布时间】:2020-10-30 15:52:58
【问题描述】:

我试图找出两个表之间的差异,我们称它们为 YestedayEmployees 和 TodaysEmployees。我目前正在获取所有两个表,然后检查每个项目以查看员工状态是否已更改或是否已被删除(它们出现在昨天的表中,但未出现在今天的表中)。当记录数量较少时这很好,但随着记录的增加,我服务器上的网络和计算开销正在成为一个问题。有没有办法在 EFCore 中作为 linq 查询来执行此操作? (甚至两个一个用于删除,一个用于更改)

【问题讨论】:

    标签: linq asp.net-core .net-core entity-framework-core ef-core-3.1


    【解决方案1】:

    请参考以下查询语句:

    测试数据(您可以使用 EF core 从数据库中获取表值,有关使用 EF core 和 asp.net MVC 的更多详细信息,请查看this link):

            List<Employee> todayEmployees = new List<Employee>()
            {
                new Employee(){ EmpID=1001, EmpName="David", Status="OT" },
                new Employee(){ EmpID=1002, EmpName="Tom", Status="Off-line" },
                new Employee(){ EmpID=1003, EmpName="Jason", Status="OT" },
                new Employee(){ EmpID = 1004, EmpName="Dick", Status="Off-line" },
                new Employee(){ EmpID = 1005, EmpName="Cece", Status="OT" },
                new Employee(){ EmpID = 1006, EmpName="Dillion", Status="OT" },
                new Employee(){ EmpID = 1007, EmpName="Jeffery", Status="Off-Line" }
            };
    
            List<Employee> yesterdayEmployees = new List<Employee>()
            {
                new Employee(){ EmpID=1001, EmpName="David", Status="OT" },
                new Employee(){ EmpID=1002, EmpName="Tom", Status="OT" },
                new Employee(){ EmpID=1003, EmpName="Jason", Status="OT"},
                new Employee(){ EmpID = 1004, EmpName="Dick", Status="OT" },
                new Employee(){ EmpID = 1005, EmpName="Cece", Status="Off-Line" }
            };
    

    要获取状态发生变化的Employee,我们可以使用Join子句和where子句来比较员工状态:

            // get the employees which changes status
            var result = (from t in todayEmployees
                          join y in yesterdayEmployees
                          on t.EmpID equals y.EmpID
                          where (t.Status != y.Status)
                          select t).ToList();
    

    输出:

            //get the employees status change information
            var query3 = (from t in todayEmployees
                          join y in yesterdayEmployees
                          on t.EmpID equals y.EmpID
                          where (t.Status != y.Status)
                          select new EmployeeViewModel()
                          {
                              EmpID = t.EmpID,
                              EmpName = t.EmpName,
                              StatusChangeLog = "change status from " + t.Status + " to " + y.Status
                          }).ToList();
    

    输出:

    要获取 TodayEmployees 表中但 YesterdayEmployee 表中不存在的员工,我们可以使用contains method 来确定序列是否包含指定元素。

            //get the employees, which in TodayEmployees Table, but not exist in the YesterdayEmployee
            var query4 = (from t in todayEmployees 
                          where !(from y in yesterdayEmployees select y.EmpID).Contains(t.EmpID)
                          select t).ToList();
    
            var query5 = todayEmployees.Where(c => !yesterdayEmployees.Select(y => y.EmpID).Contains(c.EmpID)).Select(t => t).ToList();
    

    输出:

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2020-03-22
      相关资源
      最近更新 更多