LINQ和各种连接示例

发布于 2024-11-01 19:41:28 字数 942 浏览 0 评论 0原文

我刚刚学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2024-11-08 19:41:28

LINQ 查询语法的“join ... in ... on ... into” 片段被转换为 GroupJoin()

GroupJoin() 方法,对于外部列表(或表)中的每个键,返回内部列表(或表)中具有相同键的元素列表,如果该键不存在,则返回一个空列表不存在。

因此,您的问题的左外连接代码更清晰:

如果JoinedEmpDept(即与当前检查的外部列表条目具有相同键的元素列表)为空,dept > 设置为 null(感谢 DefaultIfEmpty() 方法)。

伪代码翻译:

for each employee in ListOfEmployees  
get the list of dept having ID equal to empl.DeptID   
and set them into JoinedEmpDept  
then for each dept in JoinedEmpDept 
(if empty iterates over a single null dept)  
returns an new element containing:
employee.Name and dept.Name (or null if dept is null) 

右外连接基本上是交换了外部和内部列表的左外连接。


关于“like”问题,您应该使用string.Contains(“a”)来表示'%a%'string .StartsWith("a") 表示 'a%'string.EndsWith("a") 表示 '%a' >

示例:

var query = from el in listOfStrings
            where el.StartsWith("AB")
            select el;

编辑:

关于IN() 运算符问题...
好吧,您也可以使用 Contains() 来实现此目的,或者使用 Any()

var inGroup = new []{ "Foo", "Bar" };

var query1 = from el in listOfStrings
             where inGroup.Contains(el)
             select el;
// or equally
var query2 = from el in listOfStrings
             where inGroup.Any(x => el.Equals(x))
             select el;

The "join ... in ... on ... into" piece of LINQ query syntax, is translated into a GroupJoin().

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 to DefaultIfEmpty() method).

Translation in pseudo code:

for each employee in ListOfEmployees  
get the list of dept having ID equal to empl.DeptID   
and set them into JoinedEmpDept  
then for each dept in JoinedEmpDept 
(if empty iterates over a single null dept)  
returns an new element containing:
employee.Name and dept.Name (or null if dept is null) 

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:

var query = from el in listOfStrings
            where el.StartsWith("AB")
            select el;

EDIT:

About the IN() operator question...
well, you can use Contains() also for that, or Any():

var inGroup = new []{ "Foo", "Bar" };

var query1 = from el in listOfStrings
             where inGroup.Contains(el)
             select el;
// or equally
var query2 = from el in listOfStrings
             where inGroup.Any(x => el.Equals(x))
             select el;
温馨耳语 2024-11-08 19:41:28

左外连接之所以如此,是因为这一行:

from dept in JoinedEmpDept.DefaultIfEmpty()

它将获取所有员工,即使他们不在一个部门。当生成 SQL 时,DefaultIfEmpty 将连接转换为左外连接。

有关更多详细信息,请参阅此博客文章:C#:使用 LINQ 进行左外连接

The left outer join is so because of this line:

from dept in JoinedEmpDept.DefaultIfEmpty()

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

尸血腥色 2024-11-08 19:41:28

左连接提示,
而不是:

from user in tblUsers
join compTmp1 in tblCompanies
  on user.fkCompanyID equals compTmp1.pkCompanyID into compTmp2
from comp in compTmp2.DefaultIfEmpty()

你可以写:

from user in tblUsers
from comp in tblCompanies.Where(c => c.pkCompanyID == user.fkCompanyID).DefaultIfEmpty()

Left join Tip,
Instead of:

from user in tblUsers
join compTmp1 in tblCompanies
  on user.fkCompanyID equals compTmp1.pkCompanyID into compTmp2
from comp in compTmp2.DefaultIfEmpty()

You can write:

from user in tblUsers
from comp in tblCompanies.Where(c => c.pkCompanyID == user.fkCompanyID).DefaultIfEmpty()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文