通过Expression类进行动态构造lamda表达式。
实现了以下几种类型,好了代码说话:
public Expression<Func<T, bool>> GetAndLambdaExpression<T>(string[] keys,object[] values,string[] methons)
{
Expression expression_return = Expression.Constant(true); Type TType = typeof(T); ParameterExpression expression_param = Expression.Parameter(TType, "p"); Expression temp; for (int i = 0; i < keys.Length; i++) { switch (methons[i]) { case "=": temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), TType.GetMethod("ToString")), Expression.Constant(values[i])); expression_return = Expression.And(expression_return, temp); break; case "%": temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), typeof(string).GetMethod("Contains"), Expression.Constant(values[i], typeof(string))); expression_return = Expression.And(expression_return, temp); break; case ">": //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), // TType.GetProperty(keys[i]).GetType().GetMethod("GreaterThan"), Expression.Constant(values[i])); temp = Expression.GreaterThan(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i]))); expression_return = Expression.And(expression_return, temp); break; case "<": //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), // Expression.GetType().GetMethod("LessThan"), Expression.Constant(values[i])); temp=Expression.LessThan(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i]))); expression_return = Expression.And(expression_return, temp); break; case ">=": //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), // TType.GetProperty(keys[i]).GetType().GetMethod("GreaterThanOrEqual"), Expression.Constant(values[i])); temp = Expression.GreaterThanOrEqual(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i]))); expression_return = Expression.And(expression_return, temp); break; case "<=": //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), // TType.GetProperty(keys[i]).GetType().GetMethod("LessThanOrEqual"), Expression.Constant(values[i])); temp = Expression.LessThanOrEqual(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i]))); expression_return = Expression.And(expression_return, temp); break; case "in": string[] strarr = values[i].ToString().Split(','); Expression or_return = Expression.Constant(false); for (int k = 0; k < strarr.Length; k++) { temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])), TType.GetMethod("ToString")), Expression.Constant(strarr[k])); or_return = Expression.Or(or_return, temp); } expression_return = Expression.And(expression_return, or_return); break; } } return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
}
IList<People> peopleList = new List<People>(); for (int i = 1; i <= 6; i++) { People people = new People() { Name = "Dai_" + i.ToString(), Age = 16, Birthday = DateTime.Now, IsBuyCar = true, Sex = "F" }; peopleList.Add(people); } Expression<Func<People, bool>> func_a = GetAndLambdaExpression<People>(new string[] { "Name", "Age" }, new string[] { "Dai_2", "16" }, new string[] { "=", "<" }); var people_data = peopleList.AsEnumerable().Where(func_a.Compile()).ToList();