[英]Expression to filter data based on a property which is a list of strings
I have the following code: 我有以下代码:
case FilterQueryType.Contains:
var parameterExp = Expression.Parameter(type, "type");
var propertyExp = Expression.Property(parameterExp, filter.PropertyName);
var containsConstExp = Expression.Constant(filter.MyKeyword);
MethodInfo method = typeof(string).GetMethod("Contains", new []{typeof(string)});
var containsMethodExp = Expression.Call(propertyExp, method, containsConstExp);
var containsLambda = Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
items = items.Where(containsLambda);
break;
This code works fine as long as filter.PropertyName is a string. 只要filter.PropertyName是字符串,此代码就可以正常工作。 Now I have a case where filter.PropertyName is actually an enumerable of strings.
现在,我有一个情况,filter.PropertyName实际上是一个可枚举的字符串。
Could someone tell me how can I create the correct expression for this? 有人可以告诉我如何为此创建正确的表达式吗? (filter.MyKeyword itself will always be a single value)
(filter.MyKeyword本身将始终是单个值)
MemberExpression memberExpression = Expression.Property(parameterExp, filter.PropertyName);
Expression convertExpression = Expression.Convert(memberExpression, typeof(List<string>));
MethodCallExpression containsExpression = Expression.Call(convertExpression, "Contains", new Type[] { }, constExpr);
lambda = Expression.Lambda<Func<T, bool>>(containsExpression, parameterExp);
items = items.Where(lambda);
This solution works for me 这个解决方案对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.