/// <summary> /// 多条件获取实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <returns></returns> public T FindCondition <T>(Expression <Func <T, bool> > expression) { Type type = typeof(T); string colunmString = string.Join(",", type.GetProperties().Select(p => $"[{ p.GetMappingName()}]")); ExcepressionToSqlVisitor visitor = new ExcepressionToSqlVisitor(); { visitor.Visit(expression); string where = visitor.GetWhere(); string sql = $"select {colunmString} from [{type.GetMappingName()}] where {where}"; return(this.ExceuteSql <T>(sql, null, sqlCommand => { var reader = sqlCommand.ExecuteReader(); if (reader.Read()) { T t = Activator.CreateInstance <T>(); foreach (var prop in type.GetProperties()) { prop.SetValue(t, reader[prop.GetMappingName()] is DBNull ? null : reader[prop.GetMappingName()]); } return t; } return default(T); }, SqlConnectionStringPool.DBOperateType.Read)); } }
/// <summary> /// 条件删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <returns></returns> public bool DeleteCondition <T>(Expression <Func <T, bool> > expression) { Type type = typeof(T); ExcepressionToSqlVisitor visitor = new ExcepressionToSqlVisitor(); { visitor.Visit(expression); string where = visitor.GetWhere(); string sql = $"Delete from [{type.GetMappingName()}] where {where}"; return(this.ExceuteSql <bool>(sql, null, sqlCommand => 1 == sqlCommand.ExecuteNonQuery())); } //string sql = $"Delete from [{type.GetMappingName()}] where Id=@id"; //string sql = SqlCacheBuilder<T>.GetSql(SqlCacheBuilderType.Delete); //SqlParameter[] sqlParameters = new SqlParameter[] { new SqlParameter("@id", t.Id) }; //return this.ExceuteSql<bool>(sql, sqlParameters, sqlCommand => 1 == sqlCommand.ExecuteNonQuery()); //using (SqlConnection connection = new SqlConnection(configuationmanager)) //{ // SqlCommand command = new SqlCommand(sql, connection); // command.Parameters.AddRange(sqlParameters); // connection.Open(); // int result = command.ExecuteNonQuery(); // return result == 1; //} }