/// <summary> /// 假删除,根据表达示 /// 使用说明:: /// FalseDelete《T》(new int[]{1,2,3})或者Delete《T》(3) /// </summary> /// <param name="field">更新删除状态字段</param> /// <param name="expression">筛选表达示</param> public bool FalseDelete <T>(string field, Expression <Func <T, bool> > expression) { Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); //属性缓存 string cachePropertiesKey = "db." + typeName + ".GetProperties"; var cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance(); PropertyInfo[] props = null; if (cachePropertiesManager.ContainsKey(cachePropertiesKey)) { props = cachePropertiesManager[cachePropertiesKey]; } else { props = type.GetProperties(); cachePropertiesManager.Add(cachePropertiesKey, props, cachePropertiesManager.Day); } bool isSuccess = false; ResolveExpress re = new ResolveExpress(); re.ResolveExpression(re, expression); string sql = string.Format("UPDATE {0} SET {1}=1 WHERE 1=1 {2}", typeName, field, re.SqlWhere); int deleteRowCount = ExecuteCommand(sql, re.Paras.ToArray()); isSuccess = deleteRowCount > 0; return(isSuccess); }
/// <summary> /// 返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="expression"></param> /// <returns></returns> public static T Single <T>(this Queryable <T> queryable, Expression <Func <T, bool> > expression) { var type = queryable.Type; queryable.WhereIndex = queryable.WhereIndex + 100; ResolveExpress re = new ResolveExpress(queryable.WhereIndex); re.ResolveExpression(re, expression); queryable.Where.Add(re.SqlWhere); queryable.Params.AddRange(re.Paras); return(queryable.ToList().Single()); }
/// <summary> /// 删除,根据表达示 /// 使用说明: /// Delete《T》(it=>it.id=100) 或者Delete《T》(3) /// </summary> /// <param name="expression">筛选表达示</param> public bool Delete <T>(Expression <Func <T, bool> > expression) { Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); ResolveExpress re = new ResolveExpress(); re.ResolveExpression(re, expression); string sql = string.Format("DELETE FROM {0} WHERE 1=1 {1}", typeName, re.SqlWhere); bool isSuccess = ExecuteCommand(sql, re.Paras.ToArray()) > 0; return(isSuccess); }
/// <summary> /// 更新 /// 注意:rowObj为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列 /// 使用说明:sqlSugar.Update《T》(rowObj,whereObj); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rowObj">new T(){name="张三",sex="男"}或者new {name="张三",sex="男"}</param> /// <param name="expression">it.id=100</param> /// <returns></returns> public bool Update <T>(object rowObj, Expression <Func <T, bool> > expression) where T : class { if (rowObj == null) { throw new ArgumentNullException("SqlSugarClient.Update.rowObj"); } if (expression == null) { throw new ArgumentNullException("SqlSugarClient.Update.expression"); } Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); StringBuilder sbSql = new StringBuilder(string.Format(" UPDATE {0} SET ", typeName)); var rows = SqlSugarTool.GetParameters(rowObj); string pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName); foreach (var r in rows) { if (pkName == r.ParameterName.TrimStart('@')) { if (rowObj.GetType() == type) { continue; } } sbSql.Append(string.Format(" [{0}] =@{0} ,", r.ParameterName.TrimStart('@'))); } sbSql.Remove(sbSql.Length - 1, 1); sbSql.Append(" WHERE 1=1 "); ResolveExpress re = new ResolveExpress(); re.ResolveExpression(re, expression); sbSql.Append(re.SqlWhere);; List <SQLiteParameter> parsList = new List <SQLiteParameter>(); parsList.AddRange(re.Paras); var pars = rows; if (pars != null) { foreach (var par in pars) { //if (par.SqlDbType == SqlDbType.Udt) //{ // par.UdtTypeName = "HIERARCHYID"; //} SqlSugarTool.SetParSize(par); parsList.Add(par); } } try { var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray()); return(updateRowCount > 0); } catch (Exception ex) { var cacheManager = CacheManager <string> .GetInstance(); cacheManager.RemoveAll(it => it.Contains("KeyBy")); throw new Exception("sql:" + sbSql.ToString() + "\n" + ex.Message); } }