protected int UpdateTOnlyFields <T>(T entity, Expression <Func <T, dynamic> > onlyFields, Expression <Func <T, bool> > exp) where T : class, new() { DynamicVisitor visitor = new DynamicVisitor(); visitor.Translate(onlyFields); List <string> onlys = visitor.DynamicMembers.Select(m => m.Key).ToList(); return(UpdateT <T>(entity, onlys, exp)); }
public JoinQuery GroupBy <T1, T2, T3>(Expression <Func <T1, T2, T3, dynamic> > select = null) { DynamicVisitor visitor = new DynamicVisitor(WithAlias); visitor.Translate(select); foreach (DynamicMember c in visitor.DynamicMembers) { GroupByFields.AppendFormat("{0},", c.Field); } return(this); }
/// <summary> /// 添加降序 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="orderBy"></param> /// <returns></returns> public JoinQuery OrderByDesc <TEntity>(Expression <Func <TEntity, dynamic> > orderBy) { DynamicVisitor visitor = new DynamicVisitor(WithAlias); visitor.Translate(orderBy); foreach (DynamicMember c in visitor.DynamicMembers) { OrderByFields.AppendFormat("{0} DESC,", c.Field); } return(this); }
/// <summary> /// 忽略指定字段更新 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="ignoreFields"></param> /// <param name="exp"></param> /// <returns></returns> protected int UpdateTIgnoreFields <T>(T entity, Expression <Func <T, dynamic> > ignoreFields, Expression <Func <T, bool> > exp) where T : class, new() { try { DynamicVisitor visitor = new DynamicVisitor(); visitor.Translate(ignoreFields); List <string> ignores = visitor.DynamicMembers.Select(m => m.Key).ToList(); EntityInfo entityInfo = Caches.EntityInfoCache.Get(typeof(T)); StringBuilder updateSql = new StringBuilder(); List <DbParameter> parms = new List <DbParameter>(); updateSql.AppendFormat(" UPDATE {0} SET ", entityInfo.TableName); StringBuilder updateValues = new StringBuilder(); StringBuilder whereClause = new StringBuilder(); foreach (PropertyInfo property in entityInfo.NotKeyColumnProperties) { object propertyValue = null; if ((propertyValue = property.GetValue(entity, null)) != null && !ignores.Contains(property.Name)) { updateValues.AppendFormat("{0}={1}{2},", entityInfo.Columns[property.Name], DataBase.ParameterPrefix, property.Name); parms.Add(DataBase.GetDbParameter(property.Name, propertyValue)); } } updateSql.AppendFormat("{0} WHERE ", updateValues.ToString().TrimEnd(',')); if (exp != null) { SqlVisitor lambdaTranslator = new SqlVisitor(this.DataBase.DBType); string where = lambdaTranslator.Translate(exp); updateSql.Append(where); foreach (DbParameter parm in lambdaTranslator.Parameters) { parms.Add(parm); } return(DataBase.ExecuteSql(updateSql.ToString(), parms.ToArray())); } else { throw new LambdaLossException("进行Update操作时,lambda表达式为null"); } } catch (Exception ex) { throw ex; } }