In查询
var t1 = fsql.Select<T>()
.Where(a => new[] { 1, 2, 3 }.Contains(a.Id))
.ToList();//SELECT .. FROM ..//WHERE (a.`Id` in (1,2,3))
已优化,防止 where in 元素多过的 SQL 错误,如:
[Err] ORA-01795: maximum number of expressions in a list a 1000
原来:where id in (1..1333)
现在:where id in (1..500) or id in (501..1000) or id in (1001..1333)
In多列查询
//元组集合
vae lst = new List<(Guid, DateTime)>();
lst.Add((Guid.NewGuid(), DateTime.Now));
lst.Add((Guid.NewGuid(), DateTime.Now));var t2 = fsql.Select<T>()
.Where(a => lst.Contains(a.Id, a.ct1))
.ToList();//SELECT .. FROM ..//WHERE (a."Id" = '685ee1f6-bdf6-4719-a291-c709b8a1378f' AND a."ct1" = '2019-12-07 23:55:27' OR
//a."Id" = '5ecd838a-06a0-4c81-be43-1e77633b7404' AND a."ct1" = '2019-12-07 23:55:27')
In子表
var list2 = fsql.Select<T>()
.Where(a => fsql.Select<T>()
.ToList(b => b.Id)
.Contains(a.Id))
.ToList();// SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
// FROM `tb_topic` a
// WHERE (((cast(a.`Id` as char)) in (SELECT b.`Title`
// FROM `tb_topic` b)))
Exists子表
var list2 = fsql.Select<T>()
.Where(a => fsql.Select<T>()
.Any(b => b.Id == a.Id))
.ToList();// SELECT a.`Id`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
// FROM `xxx` a
// WHERE (exists(SELECT 1
// FROM `xxx` b
// WHERE (b.`Id` = a.`Id`)))
查找今天创建的数据
var t3 = fsql.Select<T>()
.Where(a => a.CreateTime.Date == DateTime.Today)
.ToList();//这行代码说明 FreeSql 表达式解析强大,不是所有 ORM 都支持var t4 = fsql.Select<T>()
.Where(a => a.CreateTime.Between(DateTime.Today, DateTime.Today.AddDays(1)))
.ToList();//正常用法应该是这样
SqlServer nvarchar/varchar 已兼容表达式解析,分别解析为:N'' 和 '',优化索引执行计划;
日期格式化
var t4 = fsql.Select<T>()
.First(a => a.CreateTime.ToString("HH:mm:ss");// SELECT date_format(a.`CreateTime`, '%H:%i:%s') as1
// FROM `xxx` a
// limit 0,1
v1.5.0 支持了常用 c# 日期格式化,yyyy MM dd HH mm ss yy M d H hh h m s tt t
tt t 为 AM PM
开窗函数
fsql.Select<T1, T2>()
.InnerJoin((a, b) => b.Id == a.Id)
.ToList((a, b) => new
{
Id = a.Id,
EdiId = b.Id,
over1 = SqlExt.Rank().Over().OrderBy(a.Id).OrderByDescending(b.EdiId).ToValue()
});
v1.6.0 利用自定义解析功能,增加 SqlExt.Rank().Over().PartitionBy(...) 常用的开窗函数,欢迎 PR 补充
自定义解析
[ExpressionCall]public static class DbFunc
{ //必要定义 static + ThreadLocal static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>(); public static DateTime FormatDateTime(this DateTime that, string arg1)
{ var up = context.Value; if (up.DataType == FreeSql.DataType.Sqlite) //重写内容
up.Result = $"date_format({up.ParsedContent["that"]}, {up.ParsedContent["arg1"]})"; return that;
}
}var sql1 = fsql.Select<SysModule>()
.ToSql(a => a.CreateTime.FormatDateTime("yyyy-MM-dd"));//SELECT date_format(a."CreateTime", 'yyyy-MM-dd') as1
//FROM "SysModule" a
[ExpressionCall] 特性可在静态扩展类上标记,也可以在单个静态方法上标记;
ExpressionCallContext 属性 | 类型 | 描述 |
---|---|---|
DataType | FreeSql.DataType | 用于实现不同数据库的适配判断条件 |
ParsedContent | Dictionary<string, string> | 函数的各参数解析结果 |
DbParameter | DbParameter | that 被参数化的对象(有可能为 null) |
UserParameters | List<DbParameter> | 可附加参数化对象 |
Result | string | 返回表达式函数表示的 SQL 字符串 |
当扩展方法返回值为 string 时,其返回值也可以当作 context.Value.Result 功能
当不想解析指定参数时,使用特性 [RawValue] 标记