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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
LINQtoSQL刪除實(shí)現(xiàn)體會小結(jié)

在實(shí)現(xiàn)LINQ to SQL刪除時可以使用Lambda Expression批量刪除數(shù)據(jù)那么在解析表達(dá)式過程中生成Where Condition會有大量的代碼生成,那么這里向你做一點(diǎn)簡單的總結(jié),算是一點(diǎn)體會吧,希望對你有所幫助。

在樊城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),樊城網(wǎng)站建設(shè)費(fèi)用合理。

根據(jù)LINQ to Sql原有的設(shè)計,解析Query得到DbCommand應(yīng)該是SqlProvider干的事,只是現(xiàn)在這個SqlProvider只從IReaderProvider出(事實(shí)上MS也沒設(shè)計個IUpdateProvider或者IDeleteProvider來著),所以也只對SELECT感冒。搞的咱們只能在DataContext里自力更生了。

LINQ to SQL刪除實(shí)現(xiàn)的實(shí)例:

不過既然已經(jīng)有了可以生成SELECT的IReaderProvider,稍微把SELECT語句改造一下不就能得到DELETE了嗎!基本思路:

 
 
 
  1. public static int DeleteAll﹤TEntity﹥(  
  2. this Table﹤TEntity﹥ table,   
  3. Expression﹤Func﹤TEntity, bool﹥﹥ predicate)  
  4. where TEntity : class  
  5. {  
  6. IQueryable query = table.Where(predicate);  
  7. DbCommand com = dc.GetCommand(query);  
  8.  
  9. //TODO:改造sql語句  
  10.  
  11. return com.ExecuteNonQuery();  
  12.  }  
  13. }  

這里直接拿直接拿where生成的query來GetCommand,得到的sql語句大致如下:

 
 
 
  1. SELECT fields... FROM tableName AS TableAlias WHERE Condition 

LINQ to SQL刪除的目標(biāo):

 
 
 
  1. DELETE FROM tableName WHERE Condition 

可見關(guān)鍵是得到tableName,用正則是***。不過這里還有一個缺陷就是只能用expression來做刪除不能用linq query,比如我想這樣:

 
 
 
  1. var query = from item in context.Items  
  2. where item.Name.StartsWith("XX")  
  3. select item;  
  4. context.DeleteAll(query); 

看來要把DeleteAll放到DataContext里,不過這樣有風(fēng)險,有可能會接受到無法轉(zhuǎn)換的SELECT語句,增加判斷必不可少。

LINQ to SQL刪除最終完成如下:

 
 
 
  1. public static class DataContextEx  
  2. {  
  3. public static int DeleteAll(  
  4. this DataContext dc, IQueryable query)  
  5. {  
  6. DbCommand com = dc.GetCommand(query);  
  7.  
  8. Regex reg = new Regex("^SELECT[\\s]*(?﹤Fields﹥.*)  
  9. [\\s]*FROM[\\s]*(?﹤Table﹥.*)[\\s]*AS[\\s]*  
  10. (?﹤TableAlias﹥.*)[\\s]*WHERE[\\s]*(?﹤Condition﹥.*)",  
  11. RegexOptions.IgnoreCase);  
  12.  
  13. Match match = reg.Match(com.CommandText);  
  14.  
  15. if (!match.Success)  
  16. throw new ArgumentException(  
  17. "Cannot delete this type of collection");  
  18.  
  19. string table = match.Groups["Table"].Value.Trim();  
  20. string tableAlias = match.Groups["TableAlias"].Value.Trim();  
  21. string condition = match.Groups["Condition"].  
  22. Value.Trim().Replace(tableAlias, table);  
  23.  
  24. com.CommandText = string.Format(  
  25. "DELETE FROM {0} WHERE {1}", table, condition);  
  26.  
  27. if (com.Connection.State != System.Data.ConnectionState.Open)  
  28. com.Connection.Open();  
  29.  
  30. return com.ExecuteNonQuery();  
  31. }  
  32.  
  33.  
  34. public static int DeleteAll﹤TEntity﹥(  
  35. this Table﹤TEntity﹥ table, Expression﹤Func﹤TEntity, bool﹥﹥ predicate)  
  36. where TEntity : class 
  37. {  
  38. IQueryable query = table.Where(predicate);  
  39.  
  40. return table.Context.DeleteAll(query);  
  41. }  
  42. }  

注:reg表達(dá)式取自MSDN Forum

原文來自博客園:http://www.cnblogs.com/jackielin/archive/2008/03/07/1095602.html

LINQ to SQL刪除的實(shí)現(xiàn)過程中的一點(diǎn)體會就向你介紹到這里,希望對你了解和學(xué)習(xí)LINQ to SQL刪除有所幫助。


網(wǎng)站欄目:LINQtoSQL刪除實(shí)現(xiàn)體會小結(jié)
URL地址:http://www.5511xx.com/article/copgjgh.html