本人这里写了一个,总感觉写的不合适(或说只是功能实现了,但是还有优化的地方)
并且,获取总行数的地方,还必须得加入OrderBy字句?!
并且,获取总行数的地方,还必须得加入OrderBy字句?!
// 需要查询用到的实体 BUser user = new BUser(); // 使用 OQLCompareFun 委托,完成查询条件的复用 OQLCompareFunc cmpFun = cmp => { // 此处构造复杂的Where条件 OQLCompare cmpResult = cmp.Comparer(user.BUserID, OQLCompare.CompareType.GreaterThanOrEqual, 20) & cmp.Comparer(user.BUserID, OQLCompare.CompareType.LessThanOrEqual, 40); return cmpResult; }; int maxCount = Convert.ToInt32(EntityQueryAnonymous.ExecuteOQLCount( OQL.From(user).Select().Where(cmpFun).OrderBy(user.BUserID,"ASC").END, MyDB.GetDBHelper() ) ); // 构造分页查询的对象 OQL qq = OQL.From(user) .Select() .Where(cmpFun) .END .Limit(5,2); qq.PageWithAllRecordCount = maxCount; // 以返回DataTable为例 DataTable dt = EntityQueryAnonymous.EntitysToDataTable( EntityQuery<BUser>.QueryList(qq));
解决方案
20
楼上正解, 而且关于分页查询的时候,也可以设置要查询的总记录数,很适合api接口传输
20
获取记录总数还有个重载,用于传入不同AdoHelper实例的
/// <summary> /// 取得记录总数 /// </summary> /// <param name="cp">where条件</param> /// <returns></returns> public static int GetRecordCounts<T>(OQLCompareFunc<T> cp,AdoHelper ado) where T : EntityBase, IIntID, new() { T m = new T(); OQL q = new OQL(m); if ((object)cp != null) { q.Select().Count(m.ID, "Counts").Where(cp); } else { q.Select().Count(m.ID, "Counts"); } T mCounts = EntityQuery<T>.QueryObject(q,ado); return Convert.ToInt32(mCounts.PropertyList("Counts")); }