internal static string CreateQuerySQLWithActive <T>(Expression <Func <T, bool> > expression, Expression <Func <T, object> > fieldExp = null, DapperSort sort = null) where T : class { string fieldNames = "*"; if (fieldExp != null) { FieldsFormater format = new FieldsFormater(); format.Visit(fieldExp); if (format.Parameters.Count > 0) { fieldNames = string.Join(",", format.Parameters.Select(x => x.Key)); } } var translate = new SqlTranslateFormater(); string sqlWhere = translate.Translate(expression); if (sort != null && sort.Count > 0) { sqlWhere += $" Order By {sort}"; } string tableName = GetTableName(typeof(T)); StringBuilder sqlBuilder = new StringBuilder($"select {fieldNames} from {tableName} where IsActive=1 AND "); sqlBuilder.Append(sqlWhere); return(sqlBuilder.ToString()); }
/// <summary> /// 添加条件表达式 /// </summary> /// <param name="whereExp">表达式</param> /// <returns>条件执行对象</returns> public IOperatorWhere <TModel> Where(Expression <Func <TModel, bool> > whereExp) { var translate = new SqlTranslateFormater(); string whereSql = translate.Translate(whereExp); _sql += whereSql; return(this); }
/// <summary> /// 数据是否存在 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="connection"></param> /// <param name="expression">查询条件</param> /// <param name="isActive">是否逻辑删除状态,默认否</param> /// <returns></returns> public static bool Exist <TModel>(this IDbConnection connection, Expression <Func <TModel, bool> > expression, bool isActive = false) { Type type = typeof(TModel); string tableName = GetTableName(type); var translate = new SqlTranslateFormater(); string sqlWhere = translate.Translate(expression); StringBuilder sqlBuilder = new StringBuilder($"select 1 from {tableName} where "); if (isActive) { sqlBuilder.Append(" IsActive=1 AND "); } sqlBuilder.Append(sqlWhere); return(connection.ExecuteScalar <int>(sqlBuilder.ToString()) > 0); }