linq to sql的多條件動態查詢
void ShowData()
{
using (DBDataContext db = new DBDataContext(Database.ConnectionString))
{
Expression<Func<T_Bargin, bool>> expr = n => GetCondition(n);
var _query = db.T_Bargins.Where<T_Bargin>(expr.Compile()).Select(b => new { b.F_Money, b.F_Name });
foreach (var item in _query)
{
Response.Write(item.F_Name + " " + item.F_Money.ToString() + "<br/>");
}
}
}
//得到合同金額大於6000,或合同名稱中包含"江華"字的條件
private bool GetCondition(T_Bargin _Table)
{
bool _result = false;
decimal _MinMoney = 6000;
if (_MinMoney > 0)
{
_result |= _Table.F_Money >= _MinMoney;
}
string _Name = "江華";
if (!String.IsNullOrEmpty(_Name))
{
_result |= _Table.F_Name.Contains(_Name);
}
return _result;
}
藉助老外寫的一個擴充套件表示式的類,可以把上篇中的程式碼寫得更優雅
這是PredicateBuilder的原始檔
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); } }
下面是使用示例 :
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(keyword)); } //翻譯後的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(); } List<Product> GetProductsByOR(params string[] keywords) { DBDataContext db = new DBDataContext(Database.ConnectionString); var predicate = PredicateBuilder.False<Product>(); foreach (string keyword in keywords) { string temp = keyword; predicate = predicate.Or(p => p.Description.Contains(temp)); } var query = db.Products.Where(predicate); //翻譯後的sql語句: //SELECT [t0].[ID], [t0].[Name], [t0].[Description] //FROM [dbo].[Product] AS [t0] //WHERE ([t0].[Description] LIKE '%6111%') OR ([t0].[Description] LIKE '%2350%') return query.ToList(); } 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(); } }來自:http://www.cnblogs.com/fireplume/archive/2008/04/27/1172683.html
相關文章
- SQL多條件查詢SQL
- Linq查詢之多個排序條件排序
- Linq兩個from查詢條件
- SQL中多條件查詢括號的用途SQL
- Spring data jpa 多表查詢(三:多對多關係動態條件查詢)Spring
- EntityFramework動態多條件查詢與Lambda表示式樹Framework
- Linq to Sql : 動態構造Expression進行動態查詢SQLExpress
- Laravel 多條件查詢Laravel
- .NET 通用多條件動態引數查詢方法 - SqlSugar ORMSqlSugarORM
- LINQ系列:LINQ to SQL Where條件SQL
- 查詢作為條件的SQLSQL
- C#動態查詢:巧用Expression組合多條件表示式C#Express
- sql 查詢條件問題SQL
- 多條件查詢---ssh版本
- Rafy 中的 Linq 查詢支援(根據聚合子條件查詢聚合父)
- SQL SERVER 條件語句的查詢SQLServer
- SpringBoot Jpa多條件查詢Spring Boot
- LINQ系列:LINQ to SQL Select查詢SQL
- mysql like查詢 - 根據多個條件的模糊匹配查詢MySql
- mysql帶AND關鍵字的多條件查詢MySql
- mybatis多條件的模糊查詢解決方案MyBatis
- 動態SQL-條件分頁SQL
- mysql多條件過濾查詢之mysq高階查詢MySql
- SQL SERVER 動態查詢SQLServer
- MongoDB查詢條件MongoDB
- MongoDB條件查詢MongoDB
- mysql條件查詢MySql
- 基於Solr的HBase多條件查詢測試Solr
- ASP.Net LINQ to SQl查詢ASP.NETSQL
- jsp+servlet+mysql多條件模糊查詢JSServletMySql
- 必須知道的SQL編寫技巧,多條件查詢不拼字串的寫法SQL字串
- 條件查詢JSPJS
- 寫一個“特殊”的查詢構造器 – (四、條件查詢:複雜條件)
- SQL Server解惑——查詢條件IN中能否使用變數SQLServer變數
- Laravel 多條件查詢時粗心導致的一個 BUGLaravel
- mongodb條件查詢不等於MongoDB
- golang beego orm 查詢條件 or andGolangORM
- 【mybatis-plus】條件查詢MyBatis