LINQtoSQL刪除實(shí)現(xiàn)的示例分析

LINQ to SQL刪除實(shí)現(xiàn)的示例分析,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)主營(yíng)臨海網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),臨海h5小程序制作搭建,臨海網(wǎng)站營(yíng)銷推廣歡迎臨海等地區(qū)企業(yè)咨詢

在實(shí)現(xiàn)LINQ to SQL刪除時(shí)可以使用Lambda Expression批量刪除數(shù)據(jù)那么在解析表達(dá)式過(guò)程中生成Where Condition會(huì)有大量的代碼生成。

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

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

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

public static int DeleteAll﹤TEntity﹥(  this Table﹤TEntity﹥ table,   Expression﹤Func﹤TEntity, bool﹥﹥ predicate)  where TEntity : class  {  IQueryable query = table.Where(predicate);  DbCommand com = dc.GetCommand(query);   //TODO:改造sql語(yǔ)句   return com.ExecuteNonQuery();   }  }

這里直接拿直接拿where生成的query來(lái)GetCommand,得到的sql語(yǔ)句大致如下:

SELECT fields... FROM tableName AS TableAlias WHERE Condition

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

DELETE FROM tableName WHERE Condition

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

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

看來(lái)要把DeleteAll放到DataContext里,不過(guò)這樣有風(fēng)險(xiǎn),有可能會(huì)接受到無(wú)法轉(zhuǎn)換的SELECT語(yǔ)句,增加判斷必不可少。

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

public static class DataContextEx  {  public static int DeleteAll(  this DataContext dc, IQueryable query)  {  DbCommand com = dc.GetCommand(query);   Regex reg = new Regex("^SELECT[\\s]*(?﹤Fields﹥.*)  [\\s]*FROM[\\s]*(?﹤Table﹥.*)[\\s]*AS[\\s]*  (?﹤TableAlias﹥.*)[\\s]*WHERE[\\s]*(?﹤Condition﹥.*)",  RegexOptions.IgnoreCase);   Match match = reg.Match(com.CommandText);   if (!match.Success)  throw new ArgumentException(  "Cannot delete this type of collection");   string table = match.Groups["Table"].Value.Trim();  string tableAlias = match.Groups["TableAlias"].Value.Trim();  string condition = match.Groups["Condition"].  Value.Trim().Replace(tableAlias, table);   com.CommandText = string.Format(  "DELETE FROM {0} WHERE {1}", table, condition);   if (com.Connection.State != System.Data.ConnectionState.Open)  com.Connection.Open();   return com.ExecuteNonQuery();  }    public static int DeleteAll﹤TEntity﹥(  this Table﹤TEntity﹥ table, Expression﹤Func﹤TEntity, bool﹥﹥ predicate)  where TEntity : class {  IQueryable query = table.Where(predicate);   return table.Context.DeleteAll(query);  }  }

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

關(guān)于LINQ to SQL刪除實(shí)現(xiàn)的示例分析問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)站名稱:LINQtoSQL刪除實(shí)現(xiàn)的示例分析
瀏覽路徑:http://bm7419.com/article44/pciche.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、定制開發(fā)、自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)虛擬主機(jī)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化