EntityFramework query-Join and entityframeworkjoin
First, let's take a look at Join.
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector);
The first parameter outer is the set on the left of your connection, the second inner is the set you want to connect to, and the third outerKeySelector is the key of the outer to connect, the fourth innerKeySelector is the same. The last one is the returned type. The usage is as follows:
NorthwindEntities dbContext = new NorthwindEntities();var data = dbContext.Employees.Join(dbContext.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID, Address = o.ShipAddress, EmployeeAddress = e.Address });
The preceding Code corresponds to the following SQL statement:
SELECT [Extent1].[EmployeeID] AS [EmployeeID], [Extent2].[OrderID] AS [OrderID], [Extent2].[ShipAddress] AS [ShipAddress], [Extent1].[Address] AS [Address]FROM [dbo].[Employees] AS [Extent1]INNER JOIN [dbo].[Orders] AS [Extent2] ON [Extent1].[EmployeeID] = [Extent2].[EmployeeID]
It is obvious that the internal connection method is used, but will there be other methods? Then let's look at a piece of code:
var data = dbContext.Employees.Join(dbContext.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID });
The corresponding SQL statement is as follows:
SELECT [Extent1].[EmployeeID] AS [EmployeeID], [Extent1].[OrderID] AS [OrderID]FROM [dbo].[Orders] AS [Extent1]WHERE [Extent1].[EmployeeID] IS NOT NULL
As you can see, there is no inner join, and the intention is to find the order corresponding to the Employee, but the above Code only returns the EmpoloyeeID and OrderID, because the order corresponding to the Employee is searched, it can be searched directly in Orders, so EF still optimizes the code, eliminating inner join.
Now that the connection has been written, you can call the Where extension method to filter the returned results.
Next let's take a look at GroupJoin.
public static IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector);
Let's look at an example: the Products table has a CategoryID attribute, but the Category does not store the corresponding ProductID. What should we do if we want to find the Product under the Category? As follows:
var data = dbContext.Categories.GroupJoin(dbContext.Products, c => c.CategoryID, p => p.CategoryID, (c, p) => new { CategoryID = c.CategoryID, ProductList = p });
The corresponding SQL statement is as follows:
SELECT [Project1].[CategoryID] AS [CategoryID], [Project1].[C1] AS [C1], [Project1].[ProductID] AS [ProductID], [Project1].[ProductName] AS [ProductName], [Project1].[SupplierID] AS [SupplierID], [Project1].[CategoryID1] AS [CategoryID1], [Project1].[QuantityPerUnit] AS [QuantityPerUnit], [Project1].[UnitPrice] AS [UnitPrice], [Project1].[UnitsInStock] AS [UnitsInStock], [Project1].[UnitsOnOrder] AS [UnitsOnOrder], [Project1].[ReorderLevel] AS [ReorderLevel], [Project1].[Discontinued] AS [Discontinued]FROM ( SELECT [Extent1].[CategoryID] AS [CategoryID], [Extent2].[ProductID] AS [ProductID], [Extent2].[ProductName] AS [ProductName], [Extent2].[SupplierID] AS [SupplierID], [Extent2].[CategoryID] AS [CategoryID1], [Extent2].[QuantityPerUnit] AS [QuantityPerUnit], [Extent2].[UnitPrice] AS [UnitPrice], [Extent2].[UnitsInStock] AS [UnitsInStock], [Extent2].[UnitsOnOrder] AS [UnitsOnOrder], [Extent2].[ReorderLevel] AS [ReorderLevel], [Extent2].[Discontinued] AS [Discontinued], CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1] FROM [dbo].[Categories] AS [Extent1] LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]) AS [Project1]ORDER BY [Project1].[CategoryID] ASC, [Project1].[C1] ASC