关于Linq 动态拼接查询语句有关问题
关于Linq 动态拼接查询语句问题
各位大神,小弟现在有个需求就是,根据条件来动态的拼接linq 查询语句的lamda表达式, 现在有了包含的拼接方式,差一个不包含的,请问不包含应该怎么凭借呢?
var c = Expression.Parameter(typeof(T), "m");
Expression con = null;
//包含
case "%":
//调用string的Contains方法进行比对
conNew = Expression.Call(
Expression.Property(c, typeof(T).GetProperty(q.fieldname)),
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
Expression.Constant(q.value));
break;
------解决方案--------------------
源代码
reflector反编译后的代码
看到差别了吗?Expression.Not
各位大神,小弟现在有个需求就是,根据条件来动态的拼接linq 查询语句的lamda表达式, 现在有了包含的拼接方式,差一个不包含的,请问不包含应该怎么凭借呢?
var c = Expression.Parameter(typeof(T), "m");
Expression con = null;
//包含
case "%":
//调用string的Contains方法进行比对
conNew = Expression.Call(
Expression.Property(c, typeof(T).GetProperty(q.fieldname)),
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
Expression.Constant(q.value));
break;
------解决方案--------------------
源代码
showExpression<expressionUser>(value => value.name.Contains("a"));
showExpression<expressionUser>(value => !value.name.Contains("a"));
reflector反编译后的代码
ParameterExpression CS$0$0000;
ParameterExpression CS$0$0003;
showExpression<expressionUser>(Expression.Lambda<func<expressionUser, bool>>(Expression.Call(Expression.Field(CS$0$0000 = Expression.Parameter(typeof(expressionUser), "value"), fieldof(expressionUser.name)), (MethodInfo) methodof(string.Contains), new Expression[] { Expression.Constant("a", typeof(string)) }), new ParameterExpression[] { CS$0$0000 }));
showExpression<expressionUser>(Expression.Lambda<func<expressionUser, bool>>(Expression.Not(Expression.Call(Expression.Field(CS$0$0003 = Expression.Parameter(typeof(expressionUser), "value"), fieldof(expressionUser.name)), (MethodInfo) methodof(string.Contains), new Expression[] { Expression.Constant("a", typeof(string)) })), new ParameterExpression[] { CS$0$0003 }));
看到差别了吗?Expression.Not