FreeSql using ExpressionTree optimize reading speed reading, if you must know to understand the technology in addition to native code, Emit is the fastest and ExpressionTree under .NETCore technology.
Reflecting the project during the initial period of + cache, although .NETCore optimize the performance of reflection, but after the Dapper performance comparison test and found that there are still some gaps, into ExpresstionTree after the Dapper performance considerably.
FreeSql supported types are more ExpressionTree achieve greater complexity, friends who are interested can read the source code.
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; }
}
class TestTypeParentInfo {
public int Id { get; set; }
public string Name { get; set; }
}
ISelect<Topic> select => fsql.Select<Topic>();
Returns a single record
Topic t1 = select.Where(a => a.Id == 1).ToOne();
FreeSql a convention, ToOne always return null object data or entity, ToList always returns a non-null List <entity type>
Return List
List<Topic> t1 = select.Where(a => a.Id > 0).Skip(100).Limit(200).ToList();
Back List + navigation data attributes
List<Topic> t2 = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToList();
//此时会返回普通字段 + 导航对象 Type 的数据
Returns the specified field
//返回一个字段
List<int> t3 = select.Where(a => a.Id > 0).ToList(a => a.Id);
//返回匿名类
List<匿名类> t4 = select.Where(a => a.Id > 0).ToList(a => new { a.Id, a.Title });
//返回元组
List<(int, string)> t5 = select.Where(a => a.Id > 0).ToList<(int, string)>("id, title");
//返回指定映射字段
List<(int, string)> t6 = select.Where(a => a.Id > 0).ToList<Dto>();
//返回SQL字段
List<匿名类> t7 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
.ToList(a => new {
a.Id,
a.Title,
cstitle = "substr(a.title, 0, 2)", //将 substr(a.title, 0, 2) 作为查询字段
csnow = Convert.ToDateTime("now()"), //将 now() 作为查询字段
//奇思妙想:怎么查询开窗函数的结果
});
//返回更为复杂的结构
List<匿名类> t8 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
.InnerJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => c.Id == b.ParentId)
.Where(a => b.Name != "xxx")).ToList((a, b, c) => new {
a.Id,
a.Title,
a.Type,
ccc = new { a.Id, a.Title },
tp = a.Type,
tp2 = new {
a.Id,
tp2 = a.Type.Name
},
tp3 = new {
a.Id,
tp33 = new {
a.Id
}
}
});
SQL execution
class xxx {
public int Id { get; set; }
public string Path { get; set; }
public string Title2 { get; set; }
}
List<xxx> t9 = fsql.Ado.Query<xxx>("select * from song");
List<(int, string ,string)> t10 = fsql.Ado.Query<(int, string, string)>("select * from song");
List<dynamic> t11 = fsql.Ado.Query<dynamic>("select * from song");
Using AsTable
var t12 = fsql.Select<User>()
.AsTable((a, b) => "(select * from tb_topic where clicks > 10)")
.Page(1, 10).ToList()
ToChunk
Executing the query, the returned data block, reduce memory overhead. 100000 such as reading of data, each processing 100 returns.
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
var testlist2 = new List<Song>();
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, list =>
{
testlist2.AddRange(list);
});
//这里示范,最终 testlist1 与 testlist2 返回的数据相同。
API
method | return value | parameter | description |
---|---|---|---|
ToSql | string | Returns the SQL statement to be executed | |
ToList | List
|
Execute SQL query and returns all fields of record T1 entity, if the navigation attribute exists with query returns Count returns, there is no record of when the list 0 | |
ToList<Dto> | List<Dto> | Lambda | Execute SQL query and returns the specified field or Dto mapping record, Count returned when there is no record list 0 |
ToList<T> | List<T> | string field | Executing the SQL query, returns the specified field recording field, and tuple or base type (int, string, long) receiving, recording absence Count returned list 0 |
ToOne | T1 | Executing the SQL query, returns the first record all the fields T1 entity, or null when there is no record | |
ToChunk | <Empty> | int size, Action<List<T1>> done | Executing the SQL query, the returned data block, reduce memory overhead. 100000 such as reading of data, each processing 100 returns. |
Any | bool | Execute SQL queries, whether there is record | |
Sum | T | Lambda | Specify a column summation |
me | T | Lambda | Specify a column for the minimum |
Max | T | Lambda | Specify a column selecting the maximum value |
Avg | T | Lambda | Specify a column averaging |