使用PredicateBuilder构建跨多个实体列的查询搜索 [英] Using PredicateBuilder to build query searching across multiple columns of Entity
本文介绍了使用PredicateBuilder构建跨多个实体列的查询搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字段名列表.我正在尝试建立一个谓词,以查看字段中是否包含搜索词.我已经完成了
I have a list of field names. I am trying to build a predicate to look in the fields to see if they contain the search term. I have gone done the path listed in this original question but do not understand how to do a Contains instead of a NotEqual.
string searchTerm = "Fred";
foreach (var field in FieldNames)
{
myPredicate= myPredicate.And(m => m.*field*.Contains(searchTerm));
}
到目前为止,我的代码:
My code so far:
public static Expression<Func<T, bool>> MultiColumnSearchExpression<T>(string fieldName,string searchValue)
{
var parameter = Expression.Parameter(typeof(T), "m");
var fieldAccess = Expression.PropertyOrField(parameter, fieldName);
//this next line should do a Contains rather then NotEqual but how?
var body = Expression.NotEqual(fieldAccess, nullValue);
var expr = Expression.Lambda<Func<T, bool>>(body, parameter);
return expr;
}
推荐答案
So you want to call String.Contains
method.
这是具有以下签名的String
类 instance 方法
It is a String
class instance method with the following signature
public bool Contains(string value)
可以映射到以下Expression.Call
这是方法:
public static Expression<Func<T, bool>> ContainsPredicate<T>(string memberName, string searchValue)
{
var parameter = Expression.Parameter(typeof(T), "m");
var member = Expression.PropertyOrField(parameter, memberName);
var body = Expression.Call(
member,
"Contains",
Type.EmptyTypes, // no generic type arguments
Expression.Constant(searchValue)
);
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
这篇关于使用PredicateBuilder构建跨多个实体列的查询搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文