HTML5技术

高品质开源工具Chloe.ORM:支持存储过程与Oracle - 我叫So(2)

字号+ 作者:H5之家 来源:H5之家 2016-12-08 12:00 我要评论( )

IQueryUser q = context.QueryUser();q.Select(a = AggregateFunctions.Count()).First(); /* * SELECT COUNT(1) AS "C" FROM "USERS" "USERS" WHERE ROWNUM 2 */ q.Select(a = new { Count = AggregateFunctions.C

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 这样更新字段

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • .NET Core 首例 Office 开源跨平台组件(NPOI Core) - Savorboard

    .NET Core 首例 Office 开源跨平台组件(NPOI Core) - Savorboard

    2016-12-08 13:00

  • 最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三) - 彭泽0902

    最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三) - 彭泽0902

    2016-12-08 11:00

  • 移动Web触摸与运动解决方案AlloyTouch开源啦 - 【当耐特】

    移动Web触摸与运动解决方案AlloyTouch开源啦 - 【当耐特】

    2016-12-07 15:00

  • 腾讯AlloyTeam移动Web裁剪组件AlloyCrop正式开源 - 【当耐特】

    腾讯AlloyTeam移动Web裁剪组件AlloyCrop正式开源 - 【当耐特】

    2016-11-21 13:00

网友点评
i