IQuery<User> q = context.Query<User>(); q.Select(a => AggregateFunctions.Count()).First(); /* * SELECT COUNT(1) AS "C" FROM "USERS" "USERS" WHERE ROWNUM < 2 */ q.Select(a => new { Count = AggregateFunctions.Count(), LongCount = AggregateFunctions.LongCount(), Sum = AggregateFunctions.Sum(a.Age), Max = AggregateFunctions.Max(a.Age), Min = AggregateFunctions.Min(a.Age), Average = AggregateFunctions.Average(a.Age) }).First(); /* * SELECT COUNT(1) AS "COUNT",COUNT(1) AS "LONGCOUNT",SUM("USERS"."AGE") AS "SUM",MAX("USERS"."AGE") AS "MAX",MIN("USERS"."AGE") AS "MIN",AVG("USERS"."AGE") AS "AVERAGE" FROM "USERS" "USERS" WHERE ROWNUM < 2 */ var count = q.Count(); /* * SELECT COUNT(1) AS "C" FROM "USERS" "USERS" */ var longCount = q.LongCount(); /* * SELECT COUNT(1) AS "C" FROM "USERS" "USERS" */ var sum = q.Sum(a => a.Age); /* * SELECT SUM("USERS"."AGE") AS "C" FROM "USERS" "USERS" */ var max = q.Max(a => a.Age); /* * SELECT MAX("USERS"."AGE") AS "C" FROM "USERS" "USERS" */ var min = q.Min(a => a.Age); /* * SELECT MIN("USERS"."AGE") AS "C" FROM "USERS" "USERS" */ var avg = q.Average(a => a.Age); /* * SELECT AVG("USERS"."AGE") AS "C" FROM "USERS" "USERS" */
分组查询Chloe 的分组查询功能,可以像写 sql 一样支持 Having 和 Select。
IQuery<User> q = context.Query<User>(); IGroupingQuery<User> g = q.Where(a => a.Id > 0).GroupBy(a => a.Age); g = g.Having(a => a.Age > 1 && AggregateFunctions.Count() > 0); g.Select(a => new { a.Age, Count = AggregateFunctions.Count(), Sum = AggregateFunctions.Sum(a.Age), Max = AggregateFunctions.Max(a.Age), Min = AggregateFunctions.Min(a.Age), Avg = AggregateFunctions.Average(a.Age) }).ToList(); /* * SELECT "USERS"."AGE" AS "AGE",COUNT(1) AS "COUNT",SUM("USERS"."AGE") AS "SUM",MAX("USERS"."AGE") AS "MAX",MIN("USERS"."AGE") AS "MIN",AVG("USERS"."AGE") AS "AVG" FROM "USERS" "USERS" WHERE "USERS"."ID" > 0 GROUP BY "USERS"."AGE" HAVING ("USERS"."AGE" > 1 AND COUNT(1) > 0) */
插入数据 方式1以 lambda 表达式树的方式插入:
此种方式插入的好处是,可以指定列插入,就像写 sql 一样简单。
同时,该方式插入返回表主键值。如果实体主键是自增列(序列),返回值就会是自增值。
/* User 实体打了序列标签,会自动获取序列值。返回主键 Id */ int id = (int)context.Insert<User>(() => new User() { Name = "lu", Age = 18, Gender = Gender.Man, CityId = 1, OpTime = DateTime.Now }); /* * SELECT "USERS_AUTOID"."NEXTVAL" FROM "DUAL" * Int32 :P_0 = 14; INSERT INTO "USERS"("NAME","AGE","GENDER","CITYID","OPTIME","ID") VALUES(N'lu',18,1,1,SYSTIMESTAMP,:P_0) */
方式2以实体的方式插入:
该方式插入,如果一个实体存在自增列,会自动将自增列设置到相应的属性上。
User user = new User(); user.Name = "lu"; user.Age = 18; user.Gender = Gender.Man; user.CityId = 1; user.OpTime = DateTime.Now; //会自动将自增 Id 设置到 user 的 Id 属性上 user = context.Insert(user); /* * SELECT "USERS_AUTOID"."NEXTVAL" FROM "DUAL" * Int32 :P_0 = 15; String :P_1 = 'lu'; Int32 :P_2 = 1; Int32 :P_3 = 18; DateTime :P_4 = '2016/9/5 9:16:59'; INSERT INTO "USERS"("ID","NAME","GENDER","AGE","CITYID","OPTIME") VALUES(:P_0,:P_1,:P_2,:P_3,:P_2,:P_4) */
更新数据 方式1以 lambda 表达式树的方式更新:
该方式解决的问题是:1.指定列更新;2.批量更新;3.支持类似 Age=Age + 100 这样更新字段。