新聞中心
linq動(dòng)態(tài)條件查詢總是讓人很頭疼,筆者也遇到了此類問(wèn)題,可好在網(wǎng)上信息較多,再加上筆著的實(shí)踐經(jīng)驗(yàn)較豐富,才解決了自己的問(wèn)題,拿出來(lái)和大家共享,希望也能給你帶來(lái)幫助。

創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元建昌做網(wǎng)站,已為上家服務(wù),為建昌各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
1,linq動(dòng)態(tài)條件之構(gòu)造表達(dá)式樹
- private Expression bool>> getCondition()
- {
- Expression bool>> expression = blog => true;
- if (!String.IsNullOrEmpty(Request["BlogClassID"]))
- {
- int blogClassID;
- if (Int32.TryParse(Request["BlogClassID"], out blogClassID))
- {
- Expression bool>> e2 = blog =>
- blog.BlogClass == null;
- var invokedExpr = Expression.Invoke
- (e, expression.Parameters.Cast ());
- expression = Expression.Lambda bool>>
(Expression.And(expression.Body, invokedExpr), expression.Parameters);- }
- }
- return expression;
- }
主查詢是這個(gè)樣子:
- var result = new DongBlogDataContext().Blogs.Where(getCondition());
因?yàn)楦鶕?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
這種方法是實(shí)質(zhì)是合并Lamba表達(dá)式,也就是這三句。
- 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
如果每個(gè)條件合并都這么寫會(huì)很麻煩,幸好已經(jīng)有人給寫好的輔助類:
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Collections.Generic;
- public static class PredicateBuilder
- {
- public static Expression bool>> True ()
- { return f => true; }
- public static Expression bool>> False ()
- { return f => false; }
- public static Expression bool>> Or
- (this Expression bool>> expr1,
- Expression bool>> expr2)
- {
- var invokedExpr = Expression.Invoke
- (expr2, expr1.Parameters.Cast ());
- return Expression.Lambda bool>>
- (Expression.Or (expr1.Body, invokedExpr), expr1.Parameters);
- }
- public static Expression bool>>
- And ( this Expression bool>>
- expr1,
- Expression bool>> expr2)
- {
- var invokedExpr = Expression.Invoke
- (expr2, expr1.Parameters.Cast ());
- return Expression.Lambda bool>>
- (Expression.And (expr1.Body, invokedExpr), expr1.Parameters);
- }
- }
這個(gè)類可以用于Expression >類型的表達(dá)式的合并了。
2,linq動(dòng)態(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和***個(gè)完全相同。
以上就是筆者總結(jié)的兩種linq動(dòng)態(tài)條件查詢方法,希望能夠給大家?guī)?lái)幫助。
新聞名稱:淺析linq動(dòng)態(tài)條件查詢
本文路徑:http://fisionsoft.com.cn/article/dpppcpp.html


咨詢
建站咨詢
