EFCore 获取两个表之间的差异
EFCore Getting Differences between two tables
我试图找出两个 table 之间的区别,我们称它们为 YestedayEmployees 和 TodaysEmployees。我目前正在获取所有 table,然后检查每个项目以查看员工状态是否更改或是否被删除(它们出现在昨天但不在今天的 table 中)。当记录数量较少时这很好,但随着记录的增加,我服务器上的网络和计算开销正成为一个问题。有没有办法在 EFCore 中作为 linq 查询来执行此操作? (甚至两个,一个用于删除,一个用于更改)
请参考以下查询语句:
测试数据(您可以使用 EF 核心从数据库中获取 table 值,有关将 EF 核心与 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();
输出:
要获取在TodayEmployeesTable中但在YesterdayEmployeeTable中不存在的Employees,我们可以使用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();
输出:
我试图找出两个 table 之间的区别,我们称它们为 YestedayEmployees 和 TodaysEmployees。我目前正在获取所有 table,然后检查每个项目以查看员工状态是否更改或是否被删除(它们出现在昨天但不在今天的 table 中)。当记录数量较少时这很好,但随着记录的增加,我服务器上的网络和计算开销正成为一个问题。有没有办法在 EFCore 中作为 linq 查询来执行此操作? (甚至两个,一个用于删除,一个用于更改)
请参考以下查询语句:
测试数据(您可以使用 EF 核心从数据库中获取 table 值,有关将 EF 核心与 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();
输出:
要获取在TodayEmployeesTable中但在YesterdayEmployeeTable中不存在的Employees,我们可以使用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();
输出: