配置动态linq express 静态类QueryableExtensions { 私有静态方法信息StringContainsMethod; 私有静态方法信息StringStartsWithMethod; 静态QueryableExtensions() { Type[]singleStringParam=new[]{typeof(string)}; StringContainsMethod=typeof(string).GetMethod(“Contains”,singleStringParam); StringStartWithMethod=typeof(string).GetMethod(“StartWith”,singleStringParam); } 公共静态IQueryable AppendTextFilter(此IQueryable可查询、表达式成员选择器、字符串条件、字符串值) { 表达式=null; 开关(条件) { 案例“StartsWith”: expression=expression.Call(memberSelector.Body,StringStartsWithMethod,expression.Constant(value)); 打破 案例“等于”: expression=expression.Equal(memberSelector.Body,expression.Constant(value)); 打破 案例“包含”: expression=expression.Call(memberSelector.Body,StringContainsMethod,expression.Constant(value)); 打破 违约: 抛出新的NotSupportedException(string.Format(“{0}”不是受支持的条件”,条件)); } var lambda=Expression.lambda(Expression,memberSelector.Parameters); 返回可查询的位置(λ); } }
当我在谷歌上搜索的时候,我得到了超一流的成绩。嗯,这确实帮了我很大的忙,但它仍然不能满足我的需要。配置动态linq express 静态类QueryableExtensions { 私有静态方法信息StringContainsMethod; 私有静态方法信息StringStartsWithMethod; 静态QueryableExtensions() { Type[]singleStringParam=new[]{typeof(string)}; StringContainsMethod=typeof(string).GetMethod(“Contains”,singleStringParam); StringStartWithMethod=typeof(string).GetMethod(“StartWith”,singleStringParam); } 公共静态IQueryable AppendTextFilter(此IQueryable可查询、表达式成员选择器、字符串条件、字符串值) { 表达式=null; 开关(条件) { 案例“StartsWith”: expression=expression.Call(memberSelector.Body,StringStartsWithMethod,expression.Constant(value)); 打破 案例“等于”: expression=expression.Equal(memberSelector.Body,expression.Constant(value)); 打破 案例“包含”: expression=expression.Call(memberSelector.Body,StringContainsMethod,expression.Constant(value)); 打破 违约: 抛出新的NotSupportedException(string.Format(“{0}”不是受支持的条件”,条件)); } var lambda=Expression.lambda(Expression,memberSelector.Parameters); 返回可查询的位置(λ); } },linq,dynamic,expression,Linq,Dynamic,Expression,当我在谷歌上搜索的时候,我得到了超一流的成绩。嗯,这确实帮了我很大的忙,但它仍然不能满足我的需要。 问题是它只能处理“string”类型的字段。正如您在上面的块代码中所看到的,该方法只能使用T,string进行处理。 如何在单个方法中处理所需的任何类型?好吧,这样做的想法是用泛型类型替换字符串 static class QueryableExtensions { private static MethodInfo StringContainsMethod; private static Meth
问题是它只能处理“string”类型的字段。正如您在上面的块代码中所看到的,该方法只能使用T,string进行处理。
如何在单个方法中处理所需的任何类型?好吧,这样做的想法是用泛型类型替换字符串
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);
}
}
公共静态IQueryable AppendTextFilter(
这是一个可以质疑的问题,
表达式成员选择器,
字符串条件,
t价值(价值)
但是对于您的示例来说,这没有多大意义,例如,如果
TValue
类型是string
,则可以应用StartsWith
。好的,谢谢。我对字典值不是很熟悉。这真的对我有帮助。@user1539984字典值?你什么意思?
public static IQueryable<T> AppendTextFilter<T, TValue>(
this IQueryable<T> queryable,
Expression<Func<T, TValue>> memberSelector,
string condition,
TValue value)