借助老外寫的一個擴充表達式的類,可以把上篇中的代碼寫得更優雅
這是PredicateBuilder的源檔案
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
public static class PredicateBuilder
...{
public static Expression<Func<T, bool>> True<T> () ...{ return f => true; }
public static Expression<Func<T, bool>> False<T> () ...{ return f => false; }
public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
...{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.Or (expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
(Expression.And (expr1.Body, invokedExpr), expr1.Parameters);
}
下面是使用示例 :
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
List<Product> GetProductsByAND(params string[] keywords)
DBDataContext db = new DBDataContext(Database.ConnectionString);
IQueryable<Product> query = db.Products;
foreach (string keyword in keywords)
...{
string temp = keyword;
query = query.Where(p => p.Description.Contains(temp));
}
//翻譯後的sql語句:
//Select [t0].[ID], [t0].[Name], [t0].[Description]
//FROM [dbo].[Product] AS [t0]
//Where ([t0].[Description] LIKE '%手機%') AND ([t0].[Description] LIKE '%6111%')
return query.ToList();
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
List<Product> GetProductsByOR(params string[] keywords)
var predicate = PredicateBuilder.False<Product>();
predicate = predicate.Or(p => p.Description.Contains(temp));
var query = db.Products.Where(predicate);
//Where ([t0].[Description] LIKE '%6111%') OR ([t0].[Description] LIKE '%2350%')
return query.ToList();
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuAjNhVTMlhzMzImMwQ2NjJmM1QWMkdTNlNTZllzMzYTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
void ShowData()
//var _products = GetProductsByOR("6111", "2350");
//Repeater1.DataSource = _products;
//Repeater1.DataBind();
var predicate = PredicateBuilder.True<Product>();
string _name = "6111";
if (!string.IsNullOrEmpty(_name))
predicate = predicate.And(p => p.Name.Contains(_name));
string _description = "長虹";
if (!string.IsNullOrEmpty(_description))
predicate = predicate.And(p => p.Description.Contains(_description));
using (DBDataContext db = new DBDataContext(Database.ConnectionString))
var _Products = db.Products.Where(predicate);
Repeater1.DataSource = _Products;
Repeater1.DataBind();