日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析linq動態(tài)條件查詢

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

創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目做網(wǎng)站、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元建昌做網(wǎng)站,已為上家服務,為建昌各地企業(yè)和個人服務,聯(lián)系電話:18982081108

1,linq動態(tài)條件之構(gòu)造表達式樹

 
 
 
  1. private Expression bool>> getCondition()  
  2.     {  
  3.         Expression bool>> expression = blog => true;  
  4.         if (!String.IsNullOrEmpty(Request["BlogClassID"]))  
  5.         {  
  6.             int blogClassID;  
  7.             if (Int32.TryParse(Request["BlogClassID"], out blogClassID))  
  8.             {  
  9.                 Expression bool>> e2 = blog => 
  10. blog.BlogClass == null;  
  11.                 var invokedExpr = Expression.Invoke
  12. (e, expression.Parameters.Cast ());  
  13.                 expression = Expression.Lambda bool>>
    (Expression.And(expression.Body, invokedExpr), expression.Parameters);  
  14.             }  
  15.         }  
  16.         return expression;  
  17.     } 

主查詢是這個樣子:

 
 
 
  1. var result = new DongBlogDataContext().Blogs.Where(getCondition());  

因為根據(jù)SQL追蹤,生成SQL類似:

 
 
 
  1. SELECT [t0].[BlogID], [t0].[ChannelID], 
  2. [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag],
  3.  [t0].[CreateDateTime]  
  4. FROM [dbo].[Blog] AS [t0]  
  5. WHERE [t0].[BlogClassID] IS NULL 

這種方法是實質(zhì)是合并Lamba表達式,也就是這三句。

 
 
 
  1. SELECT [t0].[BlogID], [t0].[ChannelID], 
  2. [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag], 
  3. [t0].[CreateDateTime]  
  4. FROM [dbo].[Blog] AS [t0]  
  5. WHERE [t0].[BlogClassID] IS NULL 

如果每個條件合并都這么寫會很麻煩,幸好已經(jīng)有人給寫好的輔助類:

 
 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Linq.Expressions;  
  4. using System.Collections.Generic;  
  5.  public static class PredicateBuilder  
  6. {  
  7.   public static Expression bool>> True  ()  
  8. { return f => true;  }  
  9.   public static Expression bool>> False  () 
  10. { return f => false; }  
  11.    public static Expression bool>> Or  
  12. (this Expression bool>> expr1,            
  13.                                     Expression bool>> expr2)  
  14.   {  
  15.     var invokedExpr = Expression.Invoke 
  16. (expr2, expr1.Parameters.Cast  ());  
  17.     return Expression.Lambda bool>>  
  18.           (Expression.Or (expr1.Body, invokedExpr), expr1.Parameters);  
  19.   }  
  20.    public static Expression bool>> 
  21. And  ( this Expression bool>> 
  22. expr1,  
  23.                                                        
  24. Expression bool>> expr2)  
  25.   {  
  26.     var invokedExpr = Expression.Invoke 
  27. (expr2, expr1.Parameters.Cast  ());  
  28.     return Expression.Lambda bool>>  
  29.           (Expression.And (expr1.Body, invokedExpr), expr1.Parameters);  
  30.   }  

這個類可以用于Expression >類型的表達式的合并了。

2,linq動態(tài)條件之構(gòu)造Query

同***種查詢更好的寫法:

 
 
 
  1. private IQueryable  getQuery()  
  2.     {  
  3.         IQueryable  query =  new DongBlogDataContext().Blogs;  
  4.         if (!String.IsNullOrEmpty(Request["BlogClassID"]))  
  5.         {  
  6.             int blogClassID;  
  7.             if (Int32.TryParse(Request["BlogClassID"], out blogClassID))  
  8.                 query = query.Where (blog => blog.BlogClass ==  null);  
  9.         }  
  10.         return query.OrderByDescending(blog => blog.CreateDateTime);  
  11.     } 

主查詢

 
 
 
  1. var result = getQuery(); 

生成的SQL和***個完全相同。

以上就是筆者總結(jié)的兩種linq動態(tài)條件查詢方法,希望能夠給大家?guī)韼椭?/p>
本文題目:淺析linq動態(tài)條件查詢
本文URL:http://m.5511xx.com/article/dpppcpp.html