LINQ和各种连接示例
我刚刚学习 LINQ。所以首先我需要熟悉 linq 的 join。我用 linq 在 google 上搜索左外连接和右外连接,并从代码中得到了类似
左外连接
var LeftJoin = from emp in ListOfEmployees
join dept in ListOfDepartment
on emp.DeptID equals dept.ID into JoinedEmpDept
from dept in JoinedEmpDept.DefaultIfEmpty()
select new
{
EmployeeName = emp.Name,
DepartmentName = dept != null ? dept.Name : null
};
右外连接
var RightJoin = from dept in ListOfDepartment
join employee in ListOfEmployees
on dept.ID equals employee.DeptID into joinDeptEmp
from employee in joinDeptEmp.DefaultIfEmpty()
select new
{
EmployeeName = employee != null ? employee.Name : null,
DepartmentName = dept.Name
};
的答案,我只是无法理解它是如何左外连接的,因为这里没有使用左外连接关键字。所以请告诉我如何理解连接是左外连接还是右外连接。
当我使用 linq 时,如何使用 like 运算符。 “a%”或“%a”或“%a%”
。我看到有一个包含方法有点不同。
请讨论两个问题。谢谢
i just learning LINQ. so first of all i need to be familiar with join with linq. i search google for left outer and right outer join with linq and i got answer like
left outer join
var LeftJoin = from emp in ListOfEmployees
join dept in ListOfDepartment
on emp.DeptID equals dept.ID into JoinedEmpDept
from dept in JoinedEmpDept.DefaultIfEmpty()
select new
{
EmployeeName = emp.Name,
DepartmentName = dept != null ? dept.Name : null
};
right outer join
var RightJoin = from dept in ListOfDepartment
join employee in ListOfEmployees
on dept.ID equals employee.DeptID into joinDeptEmp
from employee in joinDeptEmp.DefaultIfEmpty()
select new
{
EmployeeName = employee != null ? employee.Name : null,
DepartmentName = dept.Name
};
from then code i just could not understand how it is left outer join because no left outer key word is use here. so please tell me how to understand that the join is left outer join or right outer.
when i will use linq then how like operator can be use. 'a%' or '%a' or '%a%'
. i saw there is contain method which is bit different.
please discuss the two issue. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ 查询语法的“join ... in ... on ... into” 片段被转换为
GroupJoin()
。GroupJoin()
方法,对于外部列表(或表)中的每个键,返回内部列表(或表)中具有相同键的元素列表,如果该键不存在,则返回一个空列表不存在。因此,您的问题的左外连接代码更清晰:
如果
JoinedEmpDept
(即与当前检查的外部列表条目具有相同键的元素列表)为空,dept
> 设置为 null(感谢DefaultIfEmpty()
方法)。伪代码翻译:
右外连接基本上是交换了外部和内部列表的左外连接。
关于“like”问题,您应该使用
string.Contains(“a”)
来表示'%a%'
,string .StartsWith("a")
表示'a%'
,string.EndsWith("a")
表示'%a'
>示例:
编辑:
关于
IN()
运算符问题...好吧,您也可以使用
Contains()
来实现此目的,或者使用Any()
:The
"join ... in ... on ... into"
piece of LINQ query syntax, is translated into aGroupJoin()
.GroupJoin()
method, for each key in the outer list (or table), returns a list of elements in the inner list (or table) having the same key, or an empty list if such key doesn't exist.Hence, the left outer join code of your question is clearer:
If
JoinedEmpDept
(i.e. the list of elements having the same key of the current examined outer list entry) is empty,dept
is set to null (thanks toDefaultIfEmpty()
method).Translation in pseudo code:
The right outer join instead, is basically a left outer join with outer and inner lists exchanged.
About the "like" question, you should use
string.Contains("a")
for'%a%'
,string.StartsWith("a")
for'a%'
,string.EndsWith("a")
for'%a'
Example:
EDIT:
About the
IN()
operator question...well, you can use
Contains()
also for that, orAny()
:左外连接之所以如此,是因为这一行:
它将获取所有员工,即使他们不在一个部门。当生成 SQL 时,
DefaultIfEmpty
将连接转换为左外连接。有关更多详细信息,请参阅此博客文章:C#:使用 LINQ 进行左外连接
The left outer join is so because of this line:
which will get all of the employees, even if they are not in a department. The
DefaultIfEmpty
turns the join into an left outer join, when the SQL is generated.See this blog post for more details: C#: Left outer joins with LINQ
左连接提示,
而不是:
你可以写:
Left join Tip,
Instead of:
You can write: