From 5a149d626ae8bc3fa4bddbb53f8caf40f51f6da6 Mon Sep 17 00:00:00 2001 From: zs <zhousong@weben-smart.com> Date: 周三, 04 6月 2025 17:42:13 +0800 Subject: [PATCH] Merge branch 'master' of http://222.71.245.114:9086/r/HIA24016N_PipeLineDemo --- Weben_CMS专用代码生成器/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs | 110 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 89 insertions(+), 21 deletions(-) diff --git "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs" "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs" index 5240657..1dd935a 100644 --- "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs" +++ "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs" @@ -40,9 +40,13 @@ return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } - public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) + //public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) + //{ + // return first.Compose(second, Expression.And); + //} + public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { - return first.Compose(second, Expression.And); + return first.Compose(second, Expression.AndAlso); } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) @@ -77,6 +81,47 @@ return finalExpression; } + ///// <summary> + ///// (妯$硦鏌ヨ)鎷兼帴鎴� c.Name.contains("1111")||c.Code.Contains("1111")||c.Address.Contains("1111")) 褰㈠紡 + ///// </summary> + ///// <typeparam name="T"></typeparam> + ///// <param name="options"></param> + ///// <param name="fieldName"></param> + ///// <returns></returns> + //public static Expression<Func<T, bool>> GetConditionExpressionForFuzzyQuery<T>(string[] fieldNames, string fieldValue) + //{ + // try + // { + // ParameterExpression left = Expression.Parameter(typeof(T), "c");//c=> + // Expression expression = Expression.Constant(true);//淇敼涓簍rue锛岃В鍐冲叕鍏辨ā绯婃煡璇㈢殑闂 + // foreach (var fieldName in fieldNames) + // { + // try + // { + // Expression right = Expression.Call + // ( + // Expression.Property(left, typeof(T).GetProperty(fieldName)), //c.DataSourceName + // typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),// 鍙嶅皠浣跨敤.Contains()鏂规硶 + // Expression.Constant(fieldValue) // .Contains(fieldValue) + // ); + // expression = Expression.Or(right, expression);//c.AAA.contain("") || c.BBB.contain("") + // } + // catch (Exception ex) + // { + // throw new Exception($"鍙傛暟{fieldName}鍖归厤鍏抽敭瀛楁煡璇㈡椂澶辫触:" + ex.Message); + // } + // } + // Expression<Func<T, bool>> finalExpression + // = Expression.Lambda<Func<T, bool>>(expression, new ParameterExpression[] { left }); + // return finalExpression; + // } + // catch (Exception) + // { + // throw; + // } + //} + + /// <summary> /// (妯$硦鏌ヨ)鎷兼帴鎴� c.Name.contains("1111")||c.Code.Contains("1111")||c.Address.Contains("1111")) 褰㈠紡 /// </summary> @@ -86,35 +131,58 @@ /// <returns></returns> public static Expression<Func<T, bool>> GetConditionExpressionForFuzzyQuery<T>(string[] fieldNames, string fieldValue) { - try + if (fieldNames == null || fieldNames.Length == 0) { - ParameterExpression left = Expression.Parameter(typeof(T), "c");//c=> - Expression expression = Expression.Constant(false); - foreach (var fieldName in fieldNames) + throw new ArgumentException("鑷冲皯闇�瑕佹寚瀹氫竴涓瓧娈靛悕", nameof(fieldNames)); + } + + if (string.IsNullOrEmpty(fieldValue)) + { + // 绌哄�兼煡璇㈣繑鍥炴�绘槸杩斿洖false鐨勮〃杈惧紡 + var parameter2 = Expression.Parameter(typeof(T), "c"); + return Expression.Lambda<Func<T, bool>>(Expression.Constant(false), parameter2); + } + + ParameterExpression parameter = Expression.Parameter(typeof(T), "c"); + Expression? expression = null; + + foreach (var fieldName in fieldNames) + { + try { - try + var propertyInfo = typeof(T).GetProperty(fieldName); + + if (propertyInfo == null) { - Expression right = Expression.Call - ( - Expression.Property(left, typeof(T).GetProperty(fieldName)), //c.DataSourceName - typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),// 鍙嶅皠浣跨敤.Contains()鏂规硶 - Expression.Constant(fieldValue) // .Contains(fieldValue) - ); - expression = Expression.Or(right, expression);//c.AAA.contain("") || c.BBB.contain("") + throw new ArgumentException($"绫诲瀷 {typeof(T).Name} 涓嶅寘鍚睘鎬� {fieldName}"); } - catch (Exception ex) + + if (propertyInfo.PropertyType != typeof(string)) { - throw new Exception($"鍙傛暟{fieldName}鍖归厤鍏抽敭瀛楁煡璇㈡椂澶辫触:" + ex.Message); + throw new ArgumentException($"灞炴�� {fieldName} 涓嶆槸瀛楃涓茬被鍨�"); } + + var propertyAccess = Expression.Property(parameter, propertyInfo); + var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) }); + var searchExpression = Expression.Call(propertyAccess, containsMethod!, Expression.Constant(fieldValue)); + + expression = expression == null + ? searchExpression + : Expression.OrElse(expression, searchExpression); } - Expression<Func<T, bool>> finalExpression - = Expression.Lambda<Func<T, bool>>(expression, new ParameterExpression[] { left }); - return finalExpression; + catch (Exception ex) + { + throw new InvalidOperationException($"澶勭悊瀛楁 {fieldName} 鏃跺嚭閿�", ex); + } } - catch (Exception) + + if (expression == null) { - throw; + // 濡傛灉鎵�鏈夊瓧娈甸兘鏃犳晥锛岃繑鍥炴�绘槸杩斿洖false鐨勮〃杈惧紡 + return Expression.Lambda<Func<T, bool>>(Expression.Constant(false), parameter); } + + return Expression.Lambda<Func<T, bool>>(expression, parameter); } /// <summary> -- Gitblit v1.9.3