/// <summary> /// 动态生成Where逻辑表达式 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="member">实体的成员(不区分大小写)</param> /// <param name="logic">逻辑关系</param> /// <param name="matchValue">要匹配的值</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <returns></returns> public static Expression <Func <T, bool> > Parse <T>(string member, LogicEnum logic, string matchValue) { if (string.IsNullOrEmpty(member)) { throw new ArgumentNullException("member"); } var keyProperty = typeof(T).GetProperties().FirstOrDefault(item => item.Name.ToLower().Equals(member.Trim().ToLower())); if (keyProperty == null) { throw new ArgumentException("member不存在"); } // item var pExp = Expression.Parameter(typeof(T), "item"); // item.CreateTime Expression memberExp = Expression.MakeMemberAccess(pExp, keyProperty); if (logic != LogicEnum.Contains) { // 是否是可空类型 bool memberIsNullableType = keyProperty.PropertyType.IsGenericType && keyProperty.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)); // 是可空类型则取类型的Value属性 if (memberIsNullableType == true) { // item.CreateTime.Value memberExp = Expression.MakeMemberAccess(memberExp, keyProperty.PropertyType.GetProperty("Value")); } // 目标值类型 Type valueType = keyProperty.PropertyType; if (memberIsNullableType == true) { valueType = valueType.GetGenericArguments().FirstOrDefault(); } object value = matchValue; if (valueType.Equals(typeof(string)) == false) { // value = DateTime.Parse(matchValue) value = valueType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { value }); } var valueExp = Expression.Constant(value, valueType); // Expression.Equal var expMethod = typeof(Expression).GetMethod(logic.ToString(), new Type[] { typeof(Expression), typeof(Expression) }); // item.CreateTime.Value == value var body = expMethod.Invoke(null, new object[] { memberExp, valueExp }) as Expression; return(Expression.Lambda(body, pExp) as Expression <Func <T, bool> >); } else { // item.Member.Contains("something") var body = Expression.Call(memberExp, typeof(string).GetMethod(logic.ToString()), Expression.Constant(matchValue, typeof(string))); return(Expression.Lambda(body, pExp) as Expression <Func <T, bool> >); } }
/// <summary> /// 获取查询逻辑对应的C#方法 /// </summary> /// <param name="logicEnum"></param> /// <returns></returns> public static MethodInfo ToMethod(this LogicEnum logicEnum) { var key = logicEnum.ToString(); MethodInfo method = null; if (!logicMehtodCache.ContainsKey(key)) { lock (logic_mehtod_locker) { if (!logicMehtodCache.ContainsKey(key)) { switch (logicEnum) { case LogicEnum.Like: case LogicEnum.NoLike: method = typeof(string).GetMethod("Contains", new Type[] { typeof(string) }); break; case LogicEnum.LikeLeft: method = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }); break; case LogicEnum.LikeRight: method = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }); break; case LogicEnum.IsNullOrEmpty: default: break; } if (method == null) { throw new Exception("没找到合适的方法"); } logicMehtodCache.Add(key, method); } } } return(logicMehtodCache[key]); }