我們知道使用EF Core的Join函數可以實作SQL中的INNER JOIN,那麼怎麼實作LEFT JOIN呢?
答案就在GroupJoin、SelectMany和DefaultIfEmpty三個Linq函數的組合使用上。
下面我們舉個例子,建立一個.NET Core控制台項目,來示範使用EF Core将Person表來LEFT JOIN Products表。
Person表在EF Core中的實體類,如下:
public partial class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public DateTime? CreateTime { get; set; }
public string Flag { get; set; }
public string VarCharDescription { get; set; }
}
Products表在EF Core中的實體類,如下:
public partial class Products
{
public int Id { get; set; }
public string Product { get; set; }
}
然後Person表和Products表,在SQL Server資料庫中的資料如下所示:

最後我們要結合GroupJoin、SelectMany和DefaultIfEmpty三個Linq函數,來在EF Core上實作Person LEFT JOIN Products的SQL語句,如下所示:
class Program
{
static void Main(string[] args)
{
using (TestDBContext dbContext = new TestDBContext())
{
//Person LEFT JOIN Products
var joinResults = dbContext
.Person
.GroupJoin(dbContext.Products, person => person.Id, product => product.Id, (person, products) => new { Person = person, Products = products })
.SelectMany(combination => combination.Products.DefaultIfEmpty(), (person, products) => new { PersonId = person.Person.Id, PersonName = person.Person.Name, ProductsId = products.Id, ProductsName = products.Product }).ToList();
foreach (var joinResult in joinResults)
{
Console.WriteLine("PersonId={0}, PersonName={1}, ProductsId={2}, ProductsName={3}", joinResult.PersonId.ToString(), joinResult.PersonName == null ? "Null" : joinResult.PersonName, joinResult.ProductsId.ToString(), joinResult.ProductsName == null ? "Null" : joinResult.ProductsName);
}
}
Console.WriteLine("Press any key to end...");
Console.ReadKey();
}
}
我們可以通過EF Core的背景日志,看到EF Core生成的SQL語句如下所示:
SELECT [p].[ID] AS [PersonId], [p].[Name] AS [PersonName], [p0].[id] AS [ProductsId], [p0].[product] AS [ProductsName]
FROM [Person] AS [p]
LEFT JOIN [products] AS [p0] ON [p].[ID] = [p0].[id]
該語句在資料庫中,執行結果如下:
然後我們可以看到我們的.NET Core程式,輸出的結果如下:
可以看到,由于EF Core中實體類Products的Id屬性為int類型,不能為null,是以EF Core将Products表中為null的行輸出為了ProductsId=0,而由于實體類Products的Product屬性為string類型,可以為null,是以EF Core将Products表中為null的行輸出為了ProductsName=Null。
是以可以看到如果要在EF Core中實作LEFT JOIN還是有點麻煩的,是以我建議如果在開發過程中我們要寫一些很複雜的LEFT JOIN、RIGHT JOIN、FULL JOIN等SQL語句,可以将這些SQL語句寫成資料庫中的視圖或存儲過程等資料庫對象,然後使用EF Core将資料庫中的視圖或存儲過程等映射為實體類,這樣比在EF Core中去構造複雜的SQL語句要友善很多。
https://www.cnblogs.com/OpenCoder/category/1132736.html