public DataTable FindTable <T, FindT>(FindT FindEntity, string orderField, bool isAsc, int pageSize, int pageIndex, out int total, string appendSql = "") where FindT : class, new() { string sql = DatabaseCommon.QueryWhereSQL <FindT>(FindEntity).ToString(); Type type = FindEntity.GetType(); string viewName = ""; var viewAttribute = type.GetCustomAttributes(true).OfType <ViewAttribute>(); var descriptionAttributes = viewAttribute as ViewAttribute[] ?? viewAttribute.ToArray(); if (descriptionAttributes.Any()) { viewName = descriptionAttributes.ToList()[0].viewName; } string tableName = string.IsNullOrWhiteSpace(viewName) ? EntityAttribute.GetEntityTable <T>() : viewName; var translateInfo = DatabaseCommon.GetTranslateValue <T>(); if (translateInfo.Count(x => x.Length > 0) > 0) { sql = DatabaseCommon.PottingSql <T>(translateInfo, sql, tableName, string.IsNullOrWhiteSpace(viewName) ? false : true); } if (!string.IsNullOrEmpty(appendSql)) { sql += appendSql; } return(FindTable(sql, orderField, isAsc, pageSize, pageIndex, out total)); }
public IQueryable <T> IQueryable <T>(string sql) where T : class, new() { string tablename = EntityAttribute.GetEntityTable <T>(); using (var dbConnection = Connection) { var translateInfo = DatabaseCommon.GetTranslateValue <T>(); if (translateInfo.Count(x => x.Length > 0) > 0) { sql = DatabaseCommon.PottingSql <T>(translateInfo, sql, tablename); } var data = dbConnection.Query <T>(sql); return(data.AsQueryable()); } }
/// <summary> /// 执行表达式 /// </summary> /// <param name="exp">表达式</param> /// <param name="operateType">操作类型</param> /// <param name="columnName">列名</param> /// <param name="newValue">值</param> /// <returns>int</returns> public static StringBuilder DealExp <T>(Expression exp, OperateType operateType, List <string> columnName = null, List <string> newValue = null) { StringBuilder sb = new StringBuilder(); string TableName = EntityAttribute.GetEntityTable <T>(); switch (operateType) { case OperateType.Select: sb.Append("Select "); sb.Append(GetConditionString(columnName)); sb.Append(" From "); sb.Append(TableName); sb.Append(" Where "); break; case OperateType.Delete: sb.Append("Delete "); sb.Append(TableName); sb.Append(" Where "); break; case OperateType.Update: sb.Append("Update "); sb.Append(TableName); sb.Append(" Set("); if (columnName != null && newValue != null && columnName.Count == newValue.Count && columnName.Count > 0) { for (int i = 0; i < columnName.Count; i++) { sb.Append(columnName[i]); sb.Append("="); sb.Append(newValue[i]); } } sb.Append(") Where "); break; case OperateType.Insert: sb.Append("Insert Into "); sb.Append(TableName); sb.Append(GetConditionString(columnName).Length > 0 ? string.Format("({0})", GetConditionString(columnName)) : string.Empty); sb.Append(" Values( "); sb.Append(GetConditionString(newValue)); sb.Append(") Where "); break; } if (ExpToSqlHelper.DealExpress(exp).Substring(0, 4).ToUpper().Equals("TRUE")) { sb.Append(" 1=1 "); sb.Append(ExpToSqlHelper.DealExpress(exp).Remove(0, 4)); } else { sb.Append(ExpToSqlHelper.DealExpress(exp)); } if (operateType == OperateType.Select) { var translateInfo = DatabaseCommon.GetTranslateValue <T>(); if (translateInfo.Count(x => x.Length > 0) > 0) { StringBuilder stringBuilder = new StringBuilder(); return(stringBuilder.Append(PottingSql <T>(translateInfo, sb.ToString(), TableName))); } } return(sb); }