linq 动态拼接查询条件 扩展方法
2012-12-11 16:09
453 查看
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)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
调用例子
protected void btnQuery_Click(object sender, EventArgs e)
{
var predicate = PredicateBuilder.True<Products>();
string sProductName = txtProductName.Text;
if (sProductName != "")
{
predicate = predicate.And(p=>p.ProductName.Contains(sProductName));
}
if (ddlCategory.SelectedIndex != 0)
{
predicate = predicate.And(p => p.CategoryID == Convert.ToInt32(ddlCategory.SelectedValue));
}
gvDemo.DataSource = dc.Products.Where(predicate).Select(p => new { p.ProductName,p.Categories.CategoryName,p.UnitPrice});
gvDemo.DataBind();
}
{
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)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
调用例子
protected void btnQuery_Click(object sender, EventArgs e)
{
var predicate = PredicateBuilder.True<Products>();
string sProductName = txtProductName.Text;
if (sProductName != "")
{
predicate = predicate.And(p=>p.ProductName.Contains(sProductName));
}
if (ddlCategory.SelectedIndex != 0)
{
predicate = predicate.And(p => p.CategoryID == Convert.ToInt32(ddlCategory.SelectedValue));
}
gvDemo.DataSource = dc.Products.Where(predicate).Select(p => new { p.ProductName,p.Categories.CategoryName,p.UnitPrice});
gvDemo.DataBind();
}
相关文章推荐
- linq 动态拼接查询条件 扩展方法
- Linq to sql 实现多条件的动态查询(方法二)
- 【整理】Linq to Entity 动态拼接查询条件(重点是OR)
- Linq 联合查询条件快捷拼接方法
- Linq to sql 实现多条件的动态查询(方法一)
- Linq to Entity 动态拼接查询条件(重点是OR)
- linq to sql 的动态条件查询方法
- 扩展Linq的Distinct方法动态根据条件进行筛选
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- [转]Linq to sql 实现多条件的动态查询(方法二)
- Linq to sql 实现多条件的动态查询(方法一)
- linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- Linq多条件查询优化---使用扩展方法
- 动态拼接LINQ查询条件的解决方案
- 常用拼接多个查询条件的方法01
- Linq To Entity 查询条件扩展
- (13)多条件查询(精确查询、模糊查询):动态拼接sql
- linq to sql的多条件动态查询
- EF-Linq 将实体对象转换为搜索条件以进行通用动态查询