LINQ to SQL: opportunistic version of batch Deletion

Source: Internet
Author: User

I saw Jeffrey the other day.Extended LINQ to SQL: Batch delete data using lambda expressions. To be honest, parse the part where the expression generates the where ConditionCodeI basically didn't understand it (the fact is that there are too many code and I really don't have the patience to read it ).

According to the original design of LINQ to SQL, dbcommand obtained by parsing query should be performed by sqlprovider, but now this sqlprovider is only from ireaderprovider (in fact, Ms has not designed an iupdateprovider or ideleteprovider ), therefore, it is only common for select. We have to be self-reliant in datacontext.

But now that you have the ireaderprovider that can generate the SELECT statement, can you get the delete statement if you modify the SELECT statement slightly! Basic Ideas:

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: Modify the SQL statement

ReturnCom. executenonquery ();
 }
}

Here, we use the query generated by where to get the command directly. The obtained SQL statement is roughly as follows:

SelectFields... FromTablenameAsTablealiasWhereCondition

 

Our goal is to transform:

Delete fromTablenameWhereCondition

 

The key to getting tablename is to use regular expressions. However, there is also a defect that only expression can be used for deletion and not LINQ query. For example, I want:

VaR Query = From Item In Context. Items
Where Item. Name. startswith ( "XX" )
Select Item;
Context. deleteall (query );

It seems that deleteall should be placed in datacontext, but there is a risk that it may accept select statements that cannot be converted, and adding judgment is essential.

The final steps are as follows:

   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 );}} 


 

Note: The reg expression is taken from msdn forum.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.