| | |
| | | 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) |
| | |
| | | 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);//ä¿®æ¹ä¸ºtrueï¼è§£å³å
Œ
±æ¨¡ç³æ¥è¯¢çé®é¢ |
| | | // 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> |
| | |
| | | /// <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> |