-
Notifications
You must be signed in to change notification settings - Fork 861
动态操作
2881099 edited this page Aug 13, 2022
·
24 revisions
fsql.Insert<object>().AsType(实体类型)
.AppendData(data)
.ExecuteAffrows();
fsql.Update<object>().AsType(实体类型)
.SetSource(data)
.ExecuteAffrows();
fsql.Select<object>().AsType(实体类型)
.Where(a => (a as BaseEntity).Id == 1)
.ExecuteAffrows();
//或者仓储
var repo = fsql.GetRepository<object>();
repo.AsType(实体类型);
repo.Insert(..);
repo.Update(..);
repo.Delete(..);
repo.InsertOrUpdate(..);
var dic = new Dictionary<string, object>();
dic.Add("id", 1);
dic.Add("name", "xxxx");
fsql.InsertDict(dic).AsTable("table1").ExecuteAffrows();
fsql.UpdateDict(dic).AsTable("table1").WherePrimary("id").ExecuteAffrows();
fsql.DeleteDict(dic).AsTable("table1").ExecuteAffrows();
fsql.InsertOrUpdateDict(dic).AsTable("table1").WherePrimary("id").ExecuteAffrows();
InsertDict/UpdateDict/DeleteDict/InsertOrUpdateDict 都支持批量操作,对应类型 List<Dictionary<string, object>>
1、ISelect.Where(string sql) 使用原生条件:
fsql.Select<Region>().Where("a.id > 0") //提示:存在SQL注入安全问题
2、ISelect.WhereDynamicFilter 方法实现动态过滤条件(与前端交互),支持的操作符:
- Contains/StartsWith/EndsWith/NotContains/NotStartsWith/NotEndsWith:包含/不包含,like '%xx%',或者 like 'xx%',或者 like '%xx'
- Equal/NotEqual:等于/不等于
- GreaterThan/GreaterThanOrEqual:大于/大于等于
- LessThan/LessThanOrEqual:小于/小于等于
- Range:范围查询
- DateRange:日期范围,有特殊处理 value[1] + 1
- Any/NotAny:是否符合 value 中任何一项(直白的说是 SQL IN)
- Custom:自定义解析
DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
""Logic"" : ""Or"",
""Filters"" :
[
{
""Field"" : ""Code"", ""Operator"" : ""NotContains"", ""Value"" : ""val1"",
""Filters"" : [{ ""Field"" : ""Name"", ""Operator"" : ""NotStartsWith"", ""Value"" : ""val2"" }]
},
{
""Field"" : ""Parent.Code"", ""Operator"" : ""Equals"", ""Value"" : ""val11"",
""Filters"" : [{ ""Field"" : ""Parent.Name"", ""Operator"" : ""Contains"", ""Value"" : ""val22"" }]
}
]
}");
fsql.Select<Region>().WhereDynamicFilter(dyfilter).ToList();
//SELECT a.""Code"", a.""Name"", a.""ParentCode"", a__Parent.""Code"" as4, a__Parent.""Name"" as5, a__Parent.""ParentCode"" as6
//FROM ""Region"" a
//LEFT JOIN ""Region"" a__Parent ON a__Parent.""Code"" = a.""ParentCode""
//WHERE (not((a.""Code"") LIKE '%val1%') AND not((a.""Name"") LIKE 'val2%') OR a__Parent.""Code"" = 'val11' AND (a__Parent.""Name"") LIKE '%val22%')
《高效理解 FreeSql WhereDynamicFilter,深入了解设计初衷》
1、ISelect.OrderBy(string sql) 使用原生排序:
fsql.Select<Region>().OrderBy("a.id desc") //提示:存在SQL注入安全问题
2、ISelect.OrderByPropertyName 使用属性名排序:
- 支持导航属性,比如 OrderByPropertyName("Parent.Code")
- 支持多表查询,比如 OrderByPropertyName("b.Code")
1、ISelect.IncludeByPropertyName 方法实现动态贪婪加载,对应 Include/IncludeMany:
fsql.Select<Region>()
.IncludeByPropertyName("Parent.Parent.Parent") //ManyToOne/OneToOne
.IncludeByPropertyName("Childs") //OneToMany/ManyToMany/PgArrayToMany
.IncludeByPropertyName("Childs", then => then
.IncludeByPropertyName("Parent.Parent")
.IncludeByPropertyName("Parent.Childs")) //向下加载,可以向下 100 层
.ToList();
2、List<TDto>.IncludeByPropertyName 扩展方法也实现了 OneToMany 动态贪婪加载:
var dtos = fsql.Select<Region>().ToList<Dto>();
//非实体类型,也可以级联加载,他们不需要配置导航属性关系
dtos.IncludeByPropertyName(
orm: fsql,
property: "Childs",
where: "ParentId=Id", //临时关系
take: 5,
select: "id,name",
then => then.IncludeByPropertyName("Parent")
);
1、ISelect.ToList 使用原生SQL返回数据:
List<(int, string)> list = fsql.Select<Region>()
.ToList<(int, string)>("a.id,a.name") //提示:存在SQL注入安全问题
2、ISelect.ToDataTableByPropertyName 使用属性名返回数据:
- 支持导航属性,比如 ToDataTableByPropertyName("Parent.Code")
- 支持多表查询,比如 ToDataTableByPropertyName("b.Code")