栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java leftjoin实现

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java leftjoin实现

我们知道使用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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号