我們知道使用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語句要方便很多。
