我有两个从数据库到对象的结果集,当我内部连接它们时,它返回交叉连接。以下是我的代码:
var nw = new NorthwindEntities();
var employee1 = (from emp in nw.Employees
join ord in nw.Orders on emp.EmployeeID equals ord.EmployeeID
where emp.EmployeeID == 5
select new
{
empID = emp.EmployeeID,
empName = emp.FirstName,
ordDate = ord.OrderDate
}).ToList();
var employee2 = (from emp in nw.Employees
join ord in nw.Orders on emp.EmployeeID equals ord.EmployeeID
where emp.EmployeeID == 5
select new
{
empID = emp.EmployeeID,
empName = emp.FirstName,
shpAddress = ord.ShipAddress
}).ToList();
var employee = from e1 in employee1
join e2 in employee2 on new { e1.empID } equals new { e2.empID }
select new
{
empID = e1.empID,
empName = e1.empName,
ordDate = e1.ordDate,
orAdd = e2.shpAddress
};
gvAll.DataSource = employee.ToList();
现在,当我将employee1 和employee2 加入员工时,我得到了这些数据集的交叉连接。任何帮助将不胜感激。
我背后的主要目标是将从数据库返回的数据临时存储在某个地方,以便稍后与从数据库检索的其他数据一起使用。
employee1 和employee2 对员工ID (5) 使用相同的查询,但employee1 有empID、empName 和OrderDate,而employee2 有empID、empName 和shipAddress。现在我想从employee1 和employee2 中取出empID、empName、OrderDate 和ShipAddress。empID 在两者中都很常见,但我得到了一个交叉连接。谢谢你。