天天看點

FreeSql (十七)聯表查詢

FreeSql在查詢資料下足了功能,鍊式查詢文法、多表查詢、表達式函數支援得非常到位。

static 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(); //請務必定義成 Singleton 單例模式

class Topic {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Title { get; set; }
    public int Clicks { get; set; }
    public DateTime CreateTime { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
}
class Category {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Name { get; set; }

    public int ParentId { get; set; }
    public CategoryType Parent { get; set; }
    public List<Topic> Topics { get; set; }
}
class CategoryType {
    public int Id { get; set; }
    public string Name { get; set; }
}
           

1、導航屬性聯表

fsql.Select<Topic>()
  .LeftJoin(a => a.Category.Id == a.CategoryId)
  .LeftJoin(a => a.Category.Parent.Id == a.Category.ParentId)
  .Where(a => a.Category.Parent.Id > 0)
  .ToList();
//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId`, a__Category.`Id` as6, a__Category.`Name`, a__Category.`ParentId`
//FROM `Topic` a
//LEFT JOIN `Category` a__Category ON a__Category.`Id` = a.`CategoryId`
//LEFT JOIN `CategoryType` a__Category__Parent ON a__Category__Parent.`Id` = a__Category.`ParentId`
           
提示:正确配置導航關系後,不再需要手工調用 LeftJoin

2、複雜聯表

fsql.Select<Topic, Category, CategoryType>()
  .LeftJoin((a,b,c) => a.CategoryId == b.Id)
  .LeftJoin((a,b,c) => b.ParentId == c.Id)
  .Where((a,b,c) => c.Id > 0)
  .ToList((a,b,c) => new { a,b,c });

//或者
fsql.Select<Topic>().From<Category, CategoryType>((s, b, c) => s
  .LeftJoin(a => a.CategoryId == b.Id)
  .LeftJoin(a => b.ParentId == c.Id))
  .Where((a,b,c) => c.Id > 0)
  .ToList((a,b,c) => new { a,b,c });
//SELECT ...
//FROM `Topic` a
//LEFT JOIN `Category` b ON a.`CategoryId` = b.`Id`
//LEFT JOIN `CategoryType` c ON b.`ParentId` = c.`Id`
//WHERE c. `Id` > 0
           

3、WithSql

fsql.Select<Topic, Category, CategoryType>()
  .WithSql(
      "select * from Topic where id=@id1",
      "select * from Category where id=@id2",
      null, //不設定 CategoryType 對應的 SQL
      new { id1 = 10, id2 = 11, id3 = 13 }
  )
  .LeftJoin((a,b,c) => a.CategoryId == b.Id)
  .LeftJoin((a,b,c) => b.ParentId == c.Id)
  .ToList();
//SELECT ...
//FROM ( select * from Topic where id=@id1 ) a
//LEFT JOIN ( select * from Category where id=@id2 ) b ON a.`CategoryId` = b.`Id`
//LEFT JOIN `CategoryType` c ON b.`ParentId` = c.`Id`
           

4、SQL聯表

fsql.Select<Topic>()
  .LeftJoin("Category b on b.Id = a.CategoryId and b.Name = @bname", new { bname = "xxx" })
  .ToList();
//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId`
//FROM `Topic` a
//LEFT JOIN Category b on b.Id = a.CategoryId and b.Name = @bname
           

延伸問題:SQL聯表 b 表的字段如何在 ToList 中指定?

.ToList(a => new
{
  bid = Convert.ToInt32("b.Id"),
  bName = "b.Name"
})
           

系列文章導航

  • (一)入門
  • (二)自動遷移實體
  • (三)實體特性
  • (四)實體特性 Fluent Api
  • (五)插入資料
  • (六)批量插入資料
  • (七)插入資料時忽略列
  • (八)插入資料時指定列
  • (九)删除資料
  • (十)更新資料
  • (十一)更新資料 Where
  • (十二)更新資料時指定列
  • (十三)更新資料時忽略列
  • (十四)批量更新資料
  • (十五)查詢資料
  • (十六)分頁查詢
  • (十七)聯表查詢
  • (十八)導航屬性
  • (十九)多表查詢
  • (二十)多表查詢 WhereCascade
  • (二十一)查詢傳回資料
  • (二十二)Dto 映射查詢
  • (二十三)分組、聚合
  • (二十四)Linq To Sql 文法使用介紹
  • (二十五)延時加載
  • (二十六)貪婪加載 Include、IncludeMany
  • (二十七)将已寫好的 SQL 語句,與實體類映射進行二次查詢
  • (二十八)事務
  • (二十九)Lambda 表達式
  • (三十)讀寫分離
  • (三十一)分表分庫
  • (三十二)Aop
  • (三十三)CodeFirst 類型映射
  • (三十四)CodeFirst 遷移說明
  • (三十五)CodeFirst 自定義特性
  • (三十六)進階 倉儲系列文檔

繼續閱讀