本篇內(nèi)容主要講解“l(fā)inq動態(tài)條件查詢?nèi)绾问褂谩?,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“l(fā)inq動態(tài)條件查詢?nèi)绾问褂谩卑?
創(chuàng)新互聯(lián)建站專業(yè)提供成都主機托管四川主機托管成都服務器托管四川服務器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機房位于中國電信/網(wǎng)通/移動機房,成都服務器托管服務有保障!
1,linq動態(tài)條件之構(gòu)造表達式樹
private Expression<Func<Blog, bool>> getCondition()
{
Expression<Func<Blog, bool>> expression = blog => true;
if (!String.IsNullOrEmpty(Request["BlogClassID"]))
{
int blogClassID;
if (Int32.TryParse(Request["BlogClassID"], out blogClassID))
{
Expression<Func<Blog, bool>> e2 = blog =>
blog.BlogClass == null;
var invokedExpr = Expression.Invoke
(e, expression.Parameters.Cast
()); expression = Expression.Lambda<Func<Blog, bool>>
(Expression.And(expression.Body, invokedExpr), expression.Parameters);}
}
return expression;
}
主查詢是這個樣子:
var result = new DongBlogDataContext().Blogs.Where(getCondition());
因為根據(jù)SQL追蹤,生成SQL類似:
SELECT [t0].[BlogID], [t0].[ChannelID], [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag], [t0].[CreateDateTime] FROM [dbo].[Blog] AS [t0] WHERE [t0].[BlogClassID] IS NULL
這種方法是實質(zhì)是合并Lamba表達式,也就是這三句。
SELECT [t0].[BlogID], [t0].[ChannelID], [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag], [t0].[CreateDateTime] FROM [dbo].[Blog] AS [t0] WHERE [t0].[BlogClassID] IS NULL
如果每個條件合并都這么寫會很麻煩,幸好已經(jīng)有人給寫好的輔助類:
using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; public static class PredicateBuilder { public static Expression<Func<T, bool>> True () { return f => true; } public static Expression<Func<T, bool>> False () { return f => false; } public static Expression<Func<T, bool>> Or (this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ()); return Expression.Lambda<Func<T, bool>> (Expression.Or (expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool>> And (this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ()); return Expression.Lambda<Func<T, bool>> (Expression.And (expr1.Body, invokedExpr), expr1.Parameters); } }
這個類可以用于Expression<Func
2,linq動態(tài)條件之構(gòu)造Query
同***種查詢更好的寫法:
private IQueryable getQuery() { IQueryable query = new DongBlogDataContext().Blogs; if (!String.IsNullOrEmpty(Request["BlogClassID"])) { int blogClassID; if (Int32.TryParse(Request["BlogClassID"], out blogClassID)) query = query.Where(blog => blog.BlogClass == null); } return query.OrderByDescending(blog => blog.CreateDateTime); }
主查詢
var result = getQuery();
生成的SQL和***個完全相同。
到此,相信大家對“l(fā)inq動態(tài)條件查詢?nèi)绾问褂谩庇辛烁畹牧私?,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
網(wǎng)站題目:linq動態(tài)條件查詢?nèi)绾问褂?/a>
本文路徑:http://bm7419.com/article38/jcijsp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、軟件開發(fā)、、網(wǎng)站建設、外貿(mào)建站、自適應網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)