Expression封装为 根据 Iqueryable,字段名,判断内容,得到包含的Iqueryable
/// <summary>
/// 泛型查找
/// </summary>
/// <param name="queryable">queryable对象,包含数据</param>
/// <param name="section">字段或属性名</param>
/// <param name="constCondition">属性或字段相比较的值</param>
public static IQueryable<T> MyLinq(IQueryable<T> queryable,string section,string constCondition)
{
Type queryableType = queryable.GetType();
Type queryableTypes = typeof(IQueryable<T>);
Type ObjType = typeof(T);
ParameterExpression parameterExpression = Expression.Parameter(typeof(IQueryable<T>), "m");
// MethodInfo arg_FE_1 = (MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(Where()));
Type genericType = typeof(Queryable);
MethodInfo mi = genericType.GetGenericMethod("Where", BindingFlags.Public, new Type[] { genericType, typeof(Expression<Func<IQueryable<T>, bool>>) });
MethodInfo arg_FE_1 = mi.MakeGenericMethod(typeof(T));
ParameterExpression parameterExpression2 = Expression.Parameter(typeof(T), "u");
Expression[] expr_84 = new Expression[2];
expr_84[0] = parameterExpression;
expr_84[1] = Expression.Quote
(
Expression.Lambda<Func<T, bool>>
(
Expression.Call
(
Expression.Property
(
parameterExpression2,
typeof(T).GetProperty(section)//(MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(get_Name()))
),
typeof(string).GetMethod("Contains"),//(MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(Contains())),
new Expression[] { Expression.Constant(constCondition, typeof(string)) }
),
new ParameterExpression[]{ parameterExpression2 }
)
);
Expression body = Expression.Call(null, arg_FE_1, expr_84);
var expression = Expression.Lambda<Func<IQueryable<T>, IQueryable<T>>>
(
body,
new ParameterExpression[] { parameterExpression }
);
var list = expression.Compile()(queryable);
return list;
}
}