如何执行 Linq to Entites 左外连接
我已经阅读了大量博客文章,但尚未找到如何在两个表之间执行 LEFT OUTER JOIN 的清晰且简单的示例。关于连接的维基百科文章 Join (SQL) 提供了这个简单的模型:
CREATE TABLE `employee` (
`LastName` varchar(25),
`DepartmentID` int(4),
UNIQUE KEY `LastName` (`LastName`)
);
CREATE TABLE `department` (
`DepartmentID` int(4),
`DepartmentName` varchar(25),
UNIQUE KEY `DepartmentID` (`DepartmentID`)
);
假设我们有一个 EmployeeSet 作为员工容器 ObjectSet
和 DepartmentSet ObjectSet
。您将如何使用 Linq 执行以下查询?
SELECT LastName, DepartmentName
FROM employee e
LEFT JOIN department d
ON e.DepartmentID = d.DepartmentID
I have read plenty of blog posts and have yet to find a clear and simple example of how to perform a LEFT OUTER JOIN between two tables. The Wikipedia article on joins Join (SQL) provides this simple model:
CREATE TABLE `employee` (
`LastName` varchar(25),
`DepartmentID` int(4),
UNIQUE KEY `LastName` (`LastName`)
);
CREATE TABLE `department` (
`DepartmentID` int(4),
`DepartmentName` varchar(25),
UNIQUE KEY `DepartmentID` (`DepartmentID`)
);
Assume we had a EmployeeSet as an employee container ObjectSet<Employee> EmployeeSet
and a DepartmentSet ObjectSet<Department> DepartmentSet
. How would you perform the following query using Linq?
SELECT LastName, DepartmentName
FROM employee e
LEFT JOIN department d
ON e.DepartmentID = d.DepartmentID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会写这个,它比
join
和 做同样的事情:I would write this, which is far simpler than
join
and does exactly the same thing:您需要使用
DefaultIfEmpty
方法:You need to use the
DefaultIfEmpty
method :