FreeSql在查詢數(shù)據(jù)下足了功能,鏈式查詢語法、多表查詢、表達式函數(shù)支持得非常到位。
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<Topic> Topics { get; set; } } class TestTypeParentInfo { public int
Id { get; set; } public string Name { get; set; } } ISelect<Topic> select =>
fsql.Select<Topic>();
利用導(dǎo)航屬性聯(lián)表
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`
沒有導(dǎo)航屬性聯(lián)表
sql = select.LeftJoin<TestTypeInfo>((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<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
.LeftJoin<TestTypeParentInfo>((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`
聯(lián)表任意查
sql = select.From<TestTypeInfo, TestTypeParentInfo>((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<Topic, TestTypeInfo, TestTypeParentInfo>() .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聯(lián)表
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
系列文章導(dǎo)航
*
(一)入門 <https://www.cnblogs.com/FreeSql/p/11531300.html>
*
(二)自動遷移實體 <https://www.cnblogs.com/FreeSql/p/11531301.html>
*
(三)實體特性 <https://www.cnblogs.com/FreeSql/p/11531302.html>
*
(四)實體特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>
*
(五)插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531306.html>
*
(六)批量插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531309.html>
*
(七)插入數(shù)據(jù)時忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>
*
(八)插入數(shù)據(jù)時指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>
*
(九)刪除數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531320.html>
*
(十)更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531321.html>
*
(十一)更新數(shù)據(jù) Where <https://www.cnblogs.com/FreeSql/p/11531324.html>
*
(十二)更新數(shù)據(jù)時指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>
*
(十三)更新數(shù)據(jù)時忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>
*
(十四)批量更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531335.html>
*
(十五)查詢數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531339.html>
*
(十六)分頁查詢 <https://www.cnblogs.com/FreeSql/p/11531341.html>
*
(十七)聯(lián)表查詢
*
(十八)導(dǎo)航屬性 <https://www.cnblogs.com/FreeSql/p/11531352.html>
*
(十九)多表查詢 <https://www.cnblogs.com/FreeSql/p/11531362.html>
*
(二十)多表查詢 WhereCascade <https://www.cnblogs.com/FreeSql/p/11531372.html>
*
(二十一)查詢返回數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531376.html>
*
(二十二)Dto 映射查詢 <https://www.cnblogs.com/FreeSql/p/11531381.html>
*
(二十三)分組、聚合 <https://www.cnblogs.com/FreeSql/p/11531384.html>
*
(二十四)Linq To Sql 語法使用介紹 <https://www.cnblogs.com/FreeSql/p/11531392.html>
*
(二十五)延時加載 <https://www.cnblogs.com/FreeSql/p/11531395.html>
*
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
<https://www.cnblogs.com/FreeSql/p/11531404.html>
*
(二十七)將已寫好的 SQL 語句,與實體類映射進行二次查詢
<https://www.cnblogs.com/FreeSql/p/11531416.html>
*
(二十八)事務(wù) <https://www.cnblogs.com/FreeSql/p/11531423.html>
*
(二十九)Lambda 表達式 <https://www.cnblogs.com/FreeSql/p/11531425.html>
*
(三十)讀寫分離 <https://www.cnblogs.com/FreeSql/p/11531430.html>
*
(三十一)分區(qū)分表 <https://www.cnblogs.com/FreeSql/p/11531435.html>
*
(三十二)Aop <https://www.cnblogs.com/FreeSql/p/11531471.html>
*
(三十三)CodeFirst 類型映射 <https://www.cnblogs.com/FreeSql/p/11531543.html>
*
(三十四)CodeFirst 遷移說明 <https://www.cnblogs.com/FreeSql/p/11531550.html>
*
(三十五)CodeFirst 自定義特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>
熱門工具 換一換