efcore inner join
时间: 2023-10-17 18:05:31 浏览: 42
Entity Framework Core (EF Core) allows you to perform inner join operation between two or more tables using LINQ queries or query syntax.
Here is an example of performing inner join using LINQ queries:
```csharp
var result = from emp in context.Employees
join dept in context.Departments
on emp.DepartmentId equals dept.DepartmentId
select new
{
EmployeeName = emp.Name,
DepartmentName = dept.Name
};
```
In this example, we are joining two tables `Employees` and `Departments` based on the `DepartmentId` column. The result will contain a collection of anonymous objects with properties `EmployeeName` and `DepartmentName`.
Here is an example of performing inner join using method syntax:
```csharp
var result = context.Employees
.Join(context.Departments,
emp => emp.DepartmentId,
dept => dept.DepartmentId,
(emp, dept) => new
{
EmployeeName = emp.Name,
DepartmentName = dept.Name
})
.ToList();
```
In this example, we are performing inner join using `Join` method. We are passing four parameters to the `Join` method - first parameter is the inner collection (`context.Departments`), second parameter is the outer collection (`context.Employees`), third parameter is the key selector for the inner collection (`dept => dept.DepartmentId`), fourth parameter is the key selector for the outer collection (`emp => emp.DepartmentId`), and the fifth parameter is the result selector (`(emp, dept) => new {...}`).
The `ToList()` method is used to execute the query and return the results as a list.
I hope this helps! Let me know if you have any further questions.