我有一个搜索表单,允许用户以几种不同的方式搜索几个不同的字段。这是我的代码示例。
var claims = from c in db.Claims select c;
switch (ddlSearchField.Text)
{
case "StartsWith":
claims = claims.Where(c => c.companyFileID.StartsWith(txtSearchBox.Text));
break;
case "Equals":
claims = claims.Where(c => c.companyFileID == txtSearchBox.Text);
break;
case "Contains":
claims = claims.Where(c => c.companyFileID.Contains(txtSearchBox.Text));
break;
}
我有大约十个不同的字段,用户可以搜索,所以我的外部开关语句非常大。必须有一种更优雅的方式来实现这一目标。
答案 0 :(得分:3)
您可以通过创建扩展方法来重构代码的某些部分。这样的事情:
static class QueryableExtensions
{
private static MethodInfo StringContainsMethod;
private static MethodInfo StringStartsWithMethod;
static QueryableExtensions()
{
Type[] singleStringParam = new[] {typeof(string)};
StringContainsMethod = typeof(string).GetMethod("Contains", singleStringParam);
StringStartsWithMethod = typeof(string).GetMethod("StartsWith", singleStringParam);
}
public static IQueryable<T> AppendTextFilter<T>(this IQueryable<T> queryable, Expression<Func<T, string>> memberSelector, string condition, string value)
{
Expression expression = null;
switch (condition)
{
case "StartsWith":
expression = Expression.Call(
memberSelector.Body,
StringStartsWithMethod,
Expression.Constant(value));
break;
case "Equals":
expression = Expression.Equal(
memberSelector.Body,
Expression.Constant(value));
break;
case "Contains":
expression = Expression.Call(
memberSelector.Body,
StringContainsMethod,
Expression.Constant(value));
break;
default:
throw new NotSupportedException(string.Format("'{0}' is not a supported condition", condition));
}
var lambda = Expression.Lambda<Func<T, bool>>(
expression,
memberSelector.Parameters);
return queryable.Where(lambda);
}
}
然后你可以这样使用它:
var claims = db.Claims
.AppendTextFilter(c => c.companyFileID, ddlSearchField.Text, txtSearchBox.Text)
.AppendTextFilter(c => c.someOtherProperty, ddlOtherSearchField.Text, txtOtherSearchBox.Text)
...;
答案 1 :(得分:0)
您是否尝试过动态LINQ?它允许您构建类似于SQL WHERE子句的基于字符串的查询语句。
答案 2 :(得分:0)
您可以制作可能谓词的字典:
Dictionary<string, Func<string, Expression<Func<Claim, bool>>>> map = new Dictionary<string, Func<string, Expression<Func<Claim, bool>>>>() {
{ "StartsWith", t => c => c.companyFileID.StartsWith(t) },
{ "Equals", t => c => c.companyFileID == t },
{ "Contains", t => c => c.companyFileID.Contains(t) }
};
可以这样使用:
var search = ddlSearchField.Text;
var text = txtSearchBox.Text;
var claims = from c in db.Claims select c;
Func<string, Expression<Func<Claim, bool>>> predicate = null;
if(dict.TryGetValue(search, out predicate))
claims = claims.Where(predicate(text));