1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| public class Utils { #region 把查询条件拼接为Extensions public static Expression<Func<TEntity, bool>> GetSerchExtensions<TEntity>(String searchJson) where TEntity : class, new() { try { var ja = (JArray)JsonConvert.DeserializeObject("[" + searchJson + "]"); var enumerableQuery = new EnumerableQuery<KeyValuePair<string, JToken>>(ja[0] as JObject); return GetSerchExtensions<TEntity>(enumerableQuery); } catch (Exception) { throw; } }
public static Expression<Func<TEntity, bool>> GetSerchExtensions<TEntity>(EnumerableQuery<KeyValuePair<string, JToken>> enumerableQuery) where TEntity : class, new() { var paramExp = Expression.Parameter(typeof(TEntity), "a"); if (null == enumerableQuery || !enumerableQuery.Any()) { var valueEqual = Expression.Constant(1); var expEqual = Expression.Equal(valueEqual, valueEqual); return Expression.Lambda<Func<TEntity, bool>>(expEqual, paramExp);
} var modeltypt = typeof(TEntity); var keyList = enumerableQuery.Select(e => e.Key).ToList();
Expression whereExp = null; keyList.ForEach(s => { var searchTypeStr = s.Substring(1, s.LastIndexOf("][", StringComparison.Ordinal) - 1); var ab = s.Substring(s.LastIndexOf("][", StringComparison.Ordinal) + 2); var joinTypeStr = ab.Remove(ab.LastIndexOf("]", StringComparison.Ordinal)); var searchField = s.Substring(s.LastIndexOf("]", StringComparison.Ordinal) + 1); var value = enumerableQuery.FirstOrDefault(v => v.Key == s).Value.ToString();
LogicOperation searchType; PredicateType joinType;
if (Enum.TryParse(searchTypeStr.ToUpper(), out searchType) && Enum.TryParse(joinTypeStr.ToUpper(), out joinType) && modeltypt.GetProperties().Any(p => String.Equals(p.Name, searchField, StringComparison.CurrentCultureIgnoreCase))) { var firstOrDefault = modeltypt.GetProperties().FirstOrDefault(p => String.Equals(p.Name, searchField, StringComparison.CurrentCultureIgnoreCase)); if (firstOrDefault == null) return; var selCol = firstOrDefault.Name; var splitList = value.Split(',').ToList(); for (var i = 0; i < splitList.Count; i++) { var val = splitList[i];
if (val == null || string.IsNullOrWhiteSpace(val)) continue; var expressionFuncEquals = PrepareConditionLambda<TEntity>(selCol, val, paramExp, searchType); whereExp = i != 0 ? (whereExp == null ? expressionFuncEquals : Expression.Or(whereExp, expressionFuncEquals)) : (joinType == PredicateType.OR ? (whereExp == null ? expressionFuncEquals : Expression.Or(whereExp, expressionFuncEquals)) : (whereExp == null ? expressionFuncEquals : Expression.And(whereExp, expressionFuncEquals))); } } }); return Expression.Lambda<Func<TEntity, bool>>(whereExp, paramExp); ; }
public static Expression<Func<TEntity, bool>> GetSerchExtensions<TEntity>(List<QueryEntity> queryEntitys) where TEntity : class, new() { var paramExp = Expression.Parameter(typeof(TEntity), "a"); if (null == queryEntitys || !queryEntitys.Any()) { var valueEqual = Expression.Constant(1); var expEqual = Expression.Equal(valueEqual, valueEqual); return Expression.Lambda<Func<TEntity, bool>>(expEqual, paramExp);
} var modeltypt = typeof(TEntity); Expression whereExp = null;
queryEntitys.ForEach(q => { LogicOperation searchType = q.LogicOperation; PredicateType joinType = q.PredicateType; var searchField = q.Column; var value = q.Value; if (modeltypt.GetProperties().Any(p => String.Equals(p.Name, searchField, StringComparison.CurrentCultureIgnoreCase))) { var firstOrDefault = modeltypt.GetProperties().FirstOrDefault(p => String.Equals(p.Name, searchField, StringComparison.CurrentCultureIgnoreCase)); if (firstOrDefault == null) return; var selCol = firstOrDefault.Name; var splitList = value.ToString().Split(',').ToList(); for (var i = 0; i < splitList.Count; i++) { if (splitList[i] == null || string.IsNullOrWhiteSpace(splitList[i])) continue; var expressionFuncEquals = PrepareConditionLambda<TEntity>(selCol, splitList[i], paramExp, searchType); whereExp = i != 0 ? (whereExp == null ? expressionFuncEquals : Expression.Or(whereExp, expressionFuncEquals)) : (joinType == PredicateType.OR ? (whereExp == null ? expressionFuncEquals : Expression.Or(whereExp, expressionFuncEquals)) : (whereExp == null ? expressionFuncEquals : Expression.And(whereExp, expressionFuncEquals))); } } }); return Expression.Lambda<Func<TEntity, bool>>(whereExp, paramExp); }
private static Expression PrepareConditionLambda<TEntity>(string name, object dateValue, ParameterExpression paramExp, LogicOperation searchType = LogicOperation.EQUAL) { if (dateValue == null) throw new ArgumentNullException("dateValue"); if (paramExp == null) throw new ArgumentNullException("paramExp"); var exp = Expression.Property(paramExp, name); var propertyType = typeof(TEntity).GetProperty(name).PropertyType; object value; if (propertyType == typeof(Guid?) || propertyType == typeof(Guid)) { value = new Guid(dateValue.ToString()); } else { value = Convert.ChangeType(dateValue, TypeHelper.GetUnNullableType(propertyType)); } Expression expEqual = null; switch (searchType) { case LogicOperation.EQUAL: var valueEqual = Expression.Constant(value, propertyType); expEqual = Expression.Equal(exp, valueEqual); break; case LogicOperation.LIKE: var containsMethod = typeof(string).GetMethod("Contains"); var valueLike = Expression.Constant(value, propertyType); expEqual = Expression.Call(exp, containsMethod, valueLike); break; } return expEqual; } #endregion }
|