From fdf4cdbf9723d9d05938fe56d2014769dd7ece4c Mon Sep 17 00:00:00 2001 From: schangxiang@126.com <schangxiang@126.com> Date: 周六, 10 5月 2025 12:17:19 +0800 Subject: [PATCH] 修复 模糊查询bug --- Weben_CMS专用代码生成器/queryExtensions/CmsQueryExtensions/Extension/DynamicSearchParameters/PredicateExtensions/PredicateExtensions.cs | 102 +++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 83 insertions(+), 19 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..d8796d5 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" @@ -77,6 +77,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 +127,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