简体   繁体   English

使用 JOIN 到 LINQ 语句的 SQL 语句

[英]SQL-statement with JOIN to LINQ-statement

How would you write the following SQL as a Linq-statement?您将如何将以下 SQL 编写为 Linq 语句?

SELECT * 
FROM Projekt i 
LEFT JOIN Projekt j on i.name=j.id

If you have for example these two classes:例如,如果您有这两个类:

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int AddressId { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string AddressLine { get; set; }
}

then with Method Syntax (MS) for LEFT OUTER JOIN we'll have this code below:然后使用LEFT OUTER JOIN方法语法(MS),我们将在下面得到以下代码:

var JoinedTables = context.Employee.GroupJoin(Address,
                                              emp => emp.AddressId,
                                              add => add.ID,
                                              (emp, add) => new { emp, add })
                                   .SelectMany(x => x.add.DefaultIfEmpty(),
                                              (employee, address) => new { employee, address }))
                                   .ToList();

But with Query Syntax (QS)但是使用查询语法(QS)

which is more understandable for humans这对人类来说更容易理解

for LEFT OUTER JOIN again all you have to do is:对于LEFT OUTER JOIN再次,您所要做的就是:

var JoinedTables = (from emp in Employee
                   join add in Address
                   on emp.AddressId equals add.ID
                   into EmployeeAddressGroup
                   from address in EmployeeAddressGroup.DefaultIfEmpty()
                   select new { employee, address }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM