/// <summary> /// 设置查询列执行查询,并且将结果集转成List《T》 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sqlable"></param> /// <param name="fileds">查询列</param> /// <param name="whereObj">SQL参数,例如:new{id=1,name="张三"}</param> /// <param name="preSql">在这语句之前可插入自定义SQL</param> /// <param name="nextSql">在这语句之后可以插自定义SQL</param> /// <returns></returns> public static List <T> SelectToList <T>(this Sqlable sqlable, string fileds, object whereObj = null, string preSql = null, string nextSql = null) where T : class { StringBuilder sbSql = new StringBuilder(sqlable.Sql.ToString()); try { Check.ArgumentNullException(sqlable.Sql, "语法错误,SelectToSql必需要在.Form后面使用"); sbSql.Insert(0, string.Format("SELECT {0} ", fileds)); sbSql.Append(" WHERE 1=1").Append(string.Join(" ", sqlable.Where)); sbSql.Append(sqlable.OrderBy); sbSql.Append(sqlable.GroupBy); var sqlParams = GetAllParas(sqlable, whereObj); if (preSql != null) { sbSql.Insert(0, preSql); } if (nextSql != null) { sbSql.Append(nextSql); } var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), sqlable.DB.GetReader(sbSql.ToString(), sqlParams), fileds); return(reval); } catch (Exception ex) { Check.Exception(true, "sql:{0} \r\n message:{1}", sbSql.ToString(), ex.Message); throw; } finally { sqlable = null; sbSql = null; } }
/// <summary> /// 设置查询列和分页参数执行查询,并且将结果集转成List《T》 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sqlable"></param> /// <param name="fileds">查询列</param> /// <param name="orderByFiled">Order By字段,可以多个</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">每页显示数量</param> /// <param name="whereObj">SQL参数,例如:new{id=1,name="张三"}</param> /// <returns></returns> public static List <T> SelectToPageList <T>(this Sqlable sqlable, string fileds, string orderByFiled, int pageIndex, int pageSize, object whereObj = null) where T : class { StringBuilder sbSql = new StringBuilder(sqlable.Sql.ToString()); try { if (pageIndex == 0) { pageIndex = 1; } Check.ArgumentNullException(sqlable.Sql, "语法错误,SelectToSql必需要在.Form后面使用"); SqlSugarTool.GetSqlableSql(sqlable, fileds, orderByFiled, pageIndex, pageSize, sbSql); var sqlParams = GetAllParas(sqlable, whereObj); var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), sqlable.DB.GetReader(sbSql.ToString(), sqlParams), fileds); return(reval); } catch (Exception ex) { Check.Exception(true, "sql:{0} \r\n message:{1}", sbSql.ToString(), ex.Message); throw; } finally { sbSql = null; sqlable = null; } }
/// <summary> /// 将Queryable转换T的集合 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="queryable">查询对象</param> /// <returns>T的集合</returns> public static List <T> ToList <T>(this Queryable <T> queryable) { StringBuilder sbSql = SqlSugarTool.GetQueryableSql <T>(queryable); var reader = queryable.DB.GetReader(sbSql.ToString(), queryable.Params.ToArray()); var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), reader, queryable.SelectValue.GetSelectFiles()); queryable = null; sbSql = null; return(reval); }
/// <summary> /// 根据SQL获取T /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="pars"></param> /// <returns></returns> public T GetSingle <T>(string sql, params SQLiteParameter[] pars) { var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), GetReader(sql, pars), null).Single(); return(reval); }
/// <summary> /// 根据SQL获取T的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="pars"></param> /// <returns></returns> public List <T> GetList <T>(string sql, params SqliteParameter[] pars) { var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), GetReader(sql, pars), null); return(reval); }