/// <summary> /// 条件筛选 /// </summary> /// <param name="expression"></param> /// <returns></returns> public IQuery <T> Where(string predicate) { var vistor = new WhereExpressionVisitor <T>(); vistor.Evaluate(null, _builder, predicate); return(this); }
/// <summary> /// 条件筛选 /// </summary> /// <param name="expression"></param> /// <returns></returns> public IQuery <T> Where(Expression <Func <T, bool> > expression) { var vistor = new WhereExpressionVisitor <T>(); if (expression != null) { vistor.Evaluate(expression, _builder); } return(this); }
/// <summary> /// 批量删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression">删除条件</param> /// <returns></returns> public virtual int Delete <T>(Expression <Func <T, bool> > expression) where T : class { if (expression == null) { return(0); } var builder = new SqlBuilder <T>(Adapter, false); var resolve = new WhereExpressionVisitor <T>(); resolve.Evaluate(expression, builder); string sql = $"DELETE {builder.Table} FROM {builder.Table} {builder.Where}"; return(Connection.Execute(sql, builder.Parameters, Transaction)); }
/// <summary> /// 批量修改 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereExpression">修改条件</param> /// <param name="updateExpression">修改字段</param> /// <returns></returns> public virtual int Update <T>(Expression <Func <T, bool> > whereExpression, Expression <Func <T, T> > updateExpression) where T : class { if (whereExpression == null) { return(0); } var builder = new SqlBuilder <T>(Adapter, false); var resolve = new WhereExpressionVisitor <T>(); resolve.Evaluate(whereExpression, builder); string set = string.Empty; var expression = (MemberInitExpression)updateExpression.Body; int i = 0; int bindingCount = expression.Bindings.Count; foreach (var binding in expression.Bindings) { i++; var name = binding.Member.Name; object value; var memberExpression = ((MemberAssignment)binding).Expression; var constantExpression = memberExpression as ConstantExpression; if (constantExpression != null) { value = constantExpression.Value; } else { var lambda = Expression.Lambda(memberExpression, null); value = lambda.Compile().DynamicInvoke(); } var column = builder.Adapter.Field(builder.Table, builder.TableAliasName, name); set += $"{column}=@{name}"; if (i < bindingCount) { set += ", "; } builder.Parameters.Add(name, value); } var table = $"{(builder.IsEnableAlias ? builder.TableAliasName : builder.Table)}"; string sql = $"UPDATE {table} SET {set} {builder.Where}"; return(Connection.Execute(sql, builder.Parameters, Transaction)); }