Dav*_*enn 3 c# iqueryable linq-to-sql c#-3.0
我看到很多类似下面的代码
var customrs = MyDataContext.Customers.Where(...);
if (!String.IsNullOrEmpty(input)) {
customers = customers.Where(c => c.Email.Contains(input));
}
Run Code Online (Sandbox Code Playgroud)
我想把它放在一个扩展方法中,在调用 IQueryable 上的 Where 之前检查输入是否有效,以便它可以像这样调用
customers = MyDataContext.Customers.Where(...)
.ContainsText(c => c.Email, input);
Run Code Online (Sandbox Code Playgroud)
我的扩展方法看起来像这样
public static IQueryable<T> ContainsText<T>(this IQueryable<T> source, Expression<Func<T, string>> selector, string text) {
if (String.IsNullOrEmpty(text) {
return source;
}
else {
//do something here
}
}
Run Code Online (Sandbox Code Playgroud)
如何在解析的表达式上调用 Contains()?或者是否有另一种方法可以返回 IQueryable 结果,其中解析的表达式包含解析的文本?
更新:用于 Linq to Sql
tvanfosson曾与建设而导致表达正确的想法这个答案到这个问题。所以为了完整起见,这里是一个完整的工作解决方案
表达式构建器
public static class ExpressionBuilder {
public static Expression<Func<T, bool>> ContainsText<T>(string propertyName, string text) {
var paramExp = Expression.Parameter(typeof(T), "type");
var propExp = Expression.Property(paramExp, propertyName);
var methodInfo = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var valueExp = Expression.Constant(text, typeof(string));
var methCall = Expression.Call(propExp, methodInfo, valueExp);
return Expression.Lambda<Func<T, bool>>(methCall, paramExp);
}
}
Run Code Online (Sandbox Code Playgroud)
扩展方法
public static class IQueryableExtensions {
public static IQueryable<T> ContainsText<T>(this IQueryable<T> source, Expression<Func<T, string>> selector, string text) {
if (source == null) {
throw new ArgumentNullException();
}
if (text.IsNullOrEmpty()) {
return source;
}
string propName = ((MemberExpression)selector.Body).Member.Name;
return source.Where(ExpressionBuilder.ContainsText<T>(propName, text));
}
}
Run Code Online (Sandbox Code Playgroud)
像这样调用
var customers = MyDataContext.Customers.Where(/* some code */)
.ContainsText(c => c.Email, input);
Run Code Online (Sandbox Code Playgroud)