/// <summary> /// 分析Lambda表达式 /// </summary> /// <typeparam name="T">方法类型</typeparam> /// <param name="predicate">Lambda表达式</param> /// <returns>结果条件对象(SQL条件和参数)</returns> public ConditionItem Convert <T>(Expression <T> predicate) { if (_condition == null) { _condition = new ConditionItem(); } var old = _condition.ConditionSql; var sql = ConvertExpression(predicate.Body); var news = CheckSingle(sql); if (string.IsNullOrEmpty(old)) { _condition.ConditionSql = news; } else if (!string.IsNullOrEmpty(news)) { _condition.ConditionSql = $"({old}) {(_mergeByAnd ? "AND" : "OR")} ({news})"; } return(_condition); }
/// <summary> /// 分析Lambda表达式 /// </summary> /// <typeparam name="T">方法类型</typeparam> /// <param name="columns">关联字段</param> /// <param name="predicate">Lambda表达式</param> /// <param name="condition">之前已解析的条件,可为空</param> /// <param name="mergeByAnd">与前面的条件(condition中已存在的)是用与还是或组合</param> /// <returns>结果条件对象(SQL条件和参数)</returns> public static ConditionItem Convert2 <T>(string[] columns, Expression <Func <T, bool> > predicate, ConditionItem condition = null, bool mergeByAnd = true) { var convert = new PredicateConvert(columns.ToDictionary(p => p, p => p)) { _condition = condition, _mergeByAnd = mergeByAnd }; return(convert.Convert(predicate)); }
/// <summary> /// 分析Lambda表达式 /// </summary> /// <typeparam name="T">方法类型</typeparam> /// <param name="map">关联字段</param> /// <param name="root">Lambda表达式</param> /// <param name="condition">之前已解析的条件,可为空</param> /// <param name="mergeByAnd">与前面的条件(condition中已存在的)是用与还是或组合</param> /// <returns>结果条件对象(SQL条件和参数)</returns> public static ConditionItem Convert <T>(Dictionary <string, string> map, LambdaItem <T> root, ConditionItem condition, bool mergeByAnd) { var convert = new PredicateConvert(map) { _condition = condition, _mergeByAnd = mergeByAnd }; if (root.Root != null) { convert.Convert(root.Root); } foreach (var ch in root.Ands) { Convert(map, ch, condition, true); } foreach (var ch in root.Ors) { Convert(map, ch, condition, false); } return(convert._condition); }
/// <summary> /// 分析Lambda表达式 /// </summary> /// <typeparam name="T">方法类型</typeparam> /// <param name="map">关联字段</param> /// <param name="predicate">Lambda表达式</param> /// <param name="condition">之前已解析的条件,可为空</param> /// <param name="mergeByAnd">与前面的条件(condition中已存在的)是用与还是或组合</param> /// <returns>结果条件对象(SQL条件和参数)</returns> public static void Convert <T>(Dictionary <string, string> map, Expression <T> predicate, ConditionItem condition, bool mergeByAnd = true) { var convert = new PredicateConvert(map) { _condition = condition, _mergeByAnd = mergeByAnd }; convert.Convert(predicate); }