EF Core中如何使用LEFT JOIN

我们知道使用EF Core的Join函数可以实现SQL中的INNER JOIN,那么怎么实现LEFT JOIN呢?

答案就在GroupJoinSelectManyDefaultIfEmpty三个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数据库中的数据如下所示:

最后我们要结合GroupJoinSelectManyDefaultIfEmpty三个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 JOINRIGHT JOINFULL JOIN等SQL语句,可以将这些SQL语句写成数据库中的视图或存储过程等数据库对象,然后使用EF Core将数据库中的视图或存储过程等映射为实体类,这样比在EF Core中去构造复杂的SQL语句要方便很多。

猜你喜欢

转载自www.cnblogs.com/OpenCoder/p/12324249.html