FreeSql (十七)联表查询

FreeSql在查询数据下足了功能,链式查询语法、多表查询、表达式函数支持得非常到位。

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
    .Build();

[Table(Name = "tb_topic")]
class Topic {
    [Column(IsIdentity = true, IsPrimary = true)]
    public int Id { get; set; }
    public int Clicks { get; set; }
    public int TestTypeInfoGuid { get; set; }
    public TestTypeInfo Type { get; set; }
    public string Title { get; set; }
    public DateTime CreateTime { get; set; }
}
class TestTypeInfo {
    public int Guid { get; set; }
    public int ParentId { get; set; }
    public TestTypeParentInfo Parent { get; set; }
    public string Name { get; set; }
    public List Topics { get; set; }
}
class TestTypeParentInfo {
    public int Id { get; set; }
    public string Name { get; set; }
}

ISelect select => fsql.Select();

利用导航属性联表

sql = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid`

sql = select
    .LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid)
    .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid` 
//LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`

没有导航属性联表

sql = select.LeftJoin((a, b) => b.Guid == a.TestTypeInfoGuid).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TestTypeInfoGuid`

sql = select
    .LeftJoin((a, b) => b.Guid == a.TestTypeInfoGuid)
    .LeftJoin((a, c) => c.Id == a.Type.ParentId).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TestTypeInfoGuid` 
//LEFT JOIN `TestTypeParentInfo` c ON c.`Id` = b.`ParentId`

联表任意查

sql = select.From((s, b, c) => s
    .LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
    .LeftJoin(a => b.ParentId == c.Id)).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` b ON a.`TestTypeInfoGuid` = b.`Guid` 
//LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`

或者:

sql = fsql.Select()
    .LeftJoin((a, b, c) => a.TestTypeInfoGuid == b.Guid)
    .LeftJoin((a, b, c) => b.ParentId == c.Id).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN `TestTypeInfo` b ON a.`TestTypeInfoGuid` = b.`Guid` 
//LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`

原生SQL联表

sql = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname", new { bname = "xxx" }).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname

系列文章导航

  • (一)入门

  • (二)自动迁移实体

  • (三)实体特性

  • (四)实体特性 Fluent Api

  • (五)插入数据

  • (六)批量插入数据

  • (七)插入数据时忽略列

  • (八)插入数据时指定列

  • (九)删除数据

  • (十)更新数据

  • (十一)更新数据 Where

  • (十二)更新数据时指定列

  • (十三)更新数据时忽略列

  • (十四)批量更新数据

  • (十五)查询数据

  • (十六)分页查询

  • (十七)联表查询

  • (十八)导航属性

  • (十九)多表查询

  • (二十)多表查询 WhereCascade

  • (二十一)查询返回数据

  • (二十二)Dto 映射查询

  • (二十三)分组、聚合

  • (二十四)Linq To Sql 语法使用介绍

  • (二十五)延时加载

  • (二十六)贪婪加载 Include、IncludeMany、Dto、ToList

  • (二十七)将已写好的 SQL 语句,与实体类映射进行二次查询

  • (二十八)事务

  • (二十九)Lambda 表达式

  • (三十)读写分离

  • (三十一)分区分表

  • (三十二)Aop

  • (三十三)CodeFirst 类型映射

  • (三十四)CodeFirst 迁移说明

  • (三十五)CodeFirst 自定义特性

你可能感兴趣的:(FreeSql (十七)联表查询)