GetExpressionRightField() публичный Метод

获取拉姆达表达式的字段值
public GetExpressionRightField ( Expression exp, SqlSugarClient db ) : string
exp System.Linq.Expressions.Expression
db SqlSugarClient 数据库访问对象
Результат string
        /// <summary>
        /// 获取最小值
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="minField">列</param>
        /// <returns></returns>
        public static object Min <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression)
        {
            ResolveExpress re       = new ResolveExpress();
            var            minField = re.GetExpressionRightField(expression);

            return(Min <T, object>(queryable, minField));
        }
        /// <summary>
        /// 条件筛选 例如:expression 为 it=>it.a  inValues值为 new list《string》{"a" ,"b"} 生成的SQL就是  a in('a','b')
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Queryable <T> In <T, FieldType>(this Queryable <T> queryable, Expression <Func <T, object> > expression, List <FieldType> inValues)
        {
            ResolveExpress re          = new ResolveExpress();
            var            InFieldName = re.GetExpressionRightField(expression);

            return(In <T, FieldType>(queryable, InFieldName, inValues));
        }
        /// <summary>
        /// 分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression">分组字段 it=>it.fieldName</param>
        /// <returns></returns>
        public static Queryable <T> GroupBy <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression)
        {
            ResolveExpress re    = new ResolveExpress();
            var            field = re.GetExpressionRightField(expression);
            var            pre   = queryable.GroupBy.IsValuable() ? "," : "";

            queryable.GroupBy += pre + field;
            return(queryable);
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression">排序字段 it=>it.fieldName </param>
        /// <param name="type">排序类型</param>
        /// <returns></returns>
        public static Queryable <T> OrderBy <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression, OrderByType type = OrderByType.asc)
        {
            ResolveExpress re    = new ResolveExpress();
            var            field = re.GetExpressionRightField(expression);
            var            pre   = queryable.OrderBy.IsValuable() ? "," : "";

            queryable.OrderBy += pre + field + " " + type.ToString().ToUpper();
            return(queryable);
        }
Пример #5
0
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="T">表实体类型</typeparam>
        /// <param name="queryable">查询对象</param>
        /// <param name="expression">排序字段 it=>it.fieldName </param>
        /// <param name="type">排序类型</param>
        /// <returns>Queryable</returns>
        public static Queryable <T> OrderBy <T>(this Queryable <T> queryable, Expression <Func <T, object> > expression, OrderByType type = OrderByType.asc)
        {
            ResolveExpress re    = new ResolveExpress();
            var            field = queryable.OrderByField = re.GetExpressionRightField(expression, queryable.DB);

            if (queryable.JoinTableValue.IsValuable())
            {
                field = re.GetExpressionRightFieldByNT(expression, queryable.DB);
            }
            var pre = queryable.OrderByValue.IsValuable() ? "," : "";

            queryable.OrderByValue += pre + field.GetTranslationSqlName() + " " + type.ToString().ToUpper();
            return(queryable);
        }
        /// <summary>
        /// 根据指定列集合批量删除数据
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <typeparam name="FiledType">指定列的类型</typeparam>
        /// <param name="expression">表达式条件</param>
        /// <param name="whereIn">批定列值的集合</param>
        /// <returns>删除成功返回true</returns>
        public bool Delete <T, FiledType>(Expression <Func <T, object> > expression, params FiledType[] whereIn)
        {
            ResolveExpress re        = new ResolveExpress();
            var            fieldName = re.GetExpressionRightField(expression);
            Type           type      = typeof(T);
            string         typeName  = type.Name;

            typeName = GetTableNameByClassType(typeName);
            //属性缓存
            string cachePropertiesKey     = "db." + type.FullName + ".GetProperties";
            var    cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance();

            PropertyInfo[] props     = SqlSugarTool.GetGetPropertiesByCache(type, cachePropertiesKey, cachePropertiesManager);
            bool           isSuccess = false;

            if (whereIn != null && whereIn.Length > 0)
            {
                string sql            = string.Format("DELETE FROM [{0}] WHERE {1} IN ({2})", typeName, fieldName, whereIn.ToJoinSqlInVal());
                int    deleteRowCount = ExecuteCommand(sql);
                isSuccess = deleteRowCount > 0;
            }
            return(isSuccess);
        }