ServiceStack.OrmLite 笔记10-group having 分页orderby等

时间:2021-02-23 23:47:03

group having 分页等

var ev = OrmLiteConfig.DialectProvider.SqlExpression();


group的使用

同sql一样,注意group分组的字段要在select里

ev.Select(c => new { c.Birthday, c.Active }).Where(rn => rn.Birthday >= new DateTime(year, 1, 1) && rn.Birthday <= lastDay).GroupBy(rn => new { rn.Birthday,rn.Active});


having的使用

ev.Select(c => new { c.Birthday, c.Active }).Where(rn => rn.Birthday >= new DateTime(year, 1, 1) && rn.Birthday <= lastDay).GroupBy(rn => new { rn.Birthday, rn.Active }).Having(x=>Sql.Max(x.Rate)>5);


orderby 排序

一升一降 先降后升 ordered by Rate Descending and Name Ascending

ev.Where().OrderBy(rn => new { at = Sql.Desc(rn.Rate), rn.Name })

也可以用这个OrderByDescending


Limit 分页

其实就是skip 然后take

ev.Limit(5); 默认是take 即top 5

ev.Limit(0, 4);// first page, page size=4;

ev.Limit(4, 4);// second page

ev.Limit().OrderBy(); // clear limit, clear orde

 ToCountStatement 总数
var expr = db.From<LetterFrequency>() .Join<LetterFrequency, LetterStat>() .Where<LetterStat>(x => x.Id > 0);

var rscount= expr.ToCountStatement();//获取expr表达式结果的总数


 ev.SelectDistinct(r => r.City);
ev.Select(r => Sql.As(Sql.Max(r.Birthday), "Birthday")); //列别名Birthday
var r2 = db.Scalar<Author, DateTime>(e => Sql.Max(e.Birthday));Author表 DateTime类型

联接

LeftJoin

db.Column < string > ( db.From < TableA > ()

.LeftJoin < TableB >((a, b) => a.Id == b.Id)

.Where < TableB > (b => b.Id == null)

.Select(a => a.Name));

Join

db.From < TableA > ()

.Join < TableB > ()

.OrderBy(x => x.Id);

CrossJoin

db.From < CrossJoinTableA > ()

.CrossJoin < CrossJoinTableB >()

.OrderBy < CrossJoinTableA >(x => x.Id)

.ThenBy < CrossJoinTableB >(x => x.Id);




调试

db.GetLastSql().Print();