/// <summary> /// 执行sql语句或存储过程 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="sql">sql语句</param> /// <param name="whereObj">命令参数对应匿名对象</param> /// <returns></returns> public List <TResult> QueryBySql <TResult>(string sql, object whereObj = null) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.SqlQuery <TResult>(sql, whereObj)); } }
/// <summary> /// 查询条数 /// </summary> /// <param name="whereExpression">条件表达式</param> /// <returns></returns> public int QueryCount(Expression <Func <T, bool> > whereExpression) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Where(whereExpression).Count()); } }
/// <summary> /// 分组查询 /// </summary> /// <typeparam name="TResult">返回的新的实体数据</typeparam> /// <param name="groupbyFiles">分组字段</param> /// <param name="selectExpression">筛选字段</param> /// <param name="whereExpression">条件表达式</param> /// <param name="orderbyStr">排序字段</param> /// <param name="whereString">where字符串</param> /// <param name="whereObj">命令参数对应匿名对象</param> /// <returns></returns> public List <TResult> QueryByGroup <TResult>(string groupbyFiles, Expression <Func <T, TResult> > selectExpression, Expression <Func <T, bool> > whereExpression, string orderbyStr, string whereString = "1=1", object whereObj = null) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Where(whereExpression).Where(whereString, whereObj).GroupBy(groupbyFiles).Select <T, TResult>(selectExpression).OrderBy(orderbyStr).ToList()); } }
/// <summary> /// 查询是否存在记录 /// </summary> /// <param name="whereExpression">条件表达式</param> /// <returns></returns> public bool Any(Expression <Func <T, bool> > whereExpression) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Any(whereExpression)); } }
/// <summary> /// 单表分页查询 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页容量</param> /// <param name="whereExpression">条件表达式</param> /// <param name="orderbyStr">排序字段</param> /// <param name="whereString">where字符串</param> /// <param name="whereObj">命令参数对应匿名对象</param> /// <returns></returns> public List <T> QueryByWherePage(int pageIndex, int pageSize, Expression <Func <T, bool> > whereExpression, string orderbyStr, string whereString = "1=1", object whereObj = null) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Where(whereExpression).Where(whereString, whereObj).OrderBy(orderbyStr).ToPageList(pageIndex, pageSize)); } }
/// <summary> /// 查询所有 /// </summary> /// <returns></returns> public List <T> QueryAll() { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().ToList()); } }
/// <summary> /// 插入数据 /// </summary> /// <param name="entity">实体对象</param> /// <param name="isIdentity">是否包含主键</param> /// <returns></returns> public object Insert(T entity, bool isIdentity = true) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Insert <T>(entity, isIdentity)); } }
/// <summary> /// 查询一个 /// </summary> /// <param name="expression">Lambda表达式</param> /// <returns></returns> public T QuerySingle(Expression <Func <T, bool> > expression) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Single(expression)); } }
/// <summary> /// 查询一个 /// </summary> /// <returns></returns> public T QuerySingle() { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Single()); } }
/// <summary> /// 更新实体所有的列 /// </summary> /// <param name="model">实体对象,主键必须是第一位</param> /// <returns></returns> public bool Update(T model) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Update(model)); } }
/// <summary> /// 查询指定个数的数据 /// </summary> /// <param name="takeNum">指定个数</param> /// <param name="whereExpression">条件表达式</param> /// <param name="orderbyStr">排序字段</param> /// <returns></returns> public List <T> QueryTakeIndex(int takeNum, Expression <Func <T, bool> > whereExpression, string orderbyStr) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { return(dbClient.Queryable <T>().Where(whereExpression).OrderBy(orderbyStr).Take(takeNum).ToList()); } }
/// <summary> /// 事务执行sql语句或存储过程 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="sql">sql语句</param> /// <param name="whereObj">命令参数对应匿名对象</param> /// <returns></returns> public List <TResult> QueryBySqlTransactions <TResult>(string sql, object whereObj = null) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.SqlQuery <TResult>(sql, whereObj)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }
/// <summary> /// 批量插入数据 /// </summary> /// <param name="entites">实体集合</param> /// <param name="isIdentity">是否包含主键</param> /// <returns></returns> public List <object> InsertRange(List <T> entites, bool isIdentity = true) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.InsertRange <T>(entites, isIdentity)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }
/// <summary> /// 假删除数据 /// </summary> /// <typeparam name="FiledType"></typeparam> /// <param name="filed">更新删除状态字段</param> /// <param name="whereIn">过滤数组</param> /// <returns></returns> public bool FalseDelete <FiledType>(string filed, params FiledType[] whereIn) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.FalseDelete <T, FiledType>(filed, whereIn)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }
/// <summary> /// 假删除数据 /// </summary> /// <param name="filed">更新删除状态字段</param> /// <param name="expression">Lambda表达式</param> /// <returns></returns> public bool FalseDelete(string filed, Expression <Func <T, bool> > expression) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.FalseDelete <T>(filed, expression)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }
/// <summary> /// 更新数据 /// </summary> /// <typeparam name="FiledType"></typeparam> /// <param name="model">entity为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列</param> /// <param name="whereIn">条件数组</param> /// <returns></returns> public bool Update <FiledType>(object model, params FiledType[] whereIn) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.Update <T, FiledType>(model, whereIn)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }
/// <summary> /// 更新数据 /// </summary> /// <param name="model">entity为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列</param> /// <param name="expression">Lambda表达式</param> /// <returns></returns> public bool Update(object model, Expression <Func <T, bool> > expression) { using (SqlSugarClient dbClient = SqlSugarInstance.GetInstance()) { try { dbClient.BeginTran(); return(dbClient.Update <T>(model, expression)); } catch (Exception ex) { dbClient.RollbackTran(); throw ex; } } }