In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
LINQ to SQL deletes the example analysis of the implementation. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
When implementing LINQ to SQL deletion, you can use Lambda Expression to delete data in batches, so generating Where Condition during parsing expressions will generate a lot of code.
According to LINQ to Sql's original design, parsing Query to get DbCommand should be the job of SqlProvider, but now this SqlProvider only comes from IReaderProvider (in fact, MS didn't design an IUpdateProvider or IDeleteProvider), so it's only a cold for SELECT. So we have to rely on ourselves in DataContext.
LINQ to SQL deletes an example of the implementation:
But now that you have an IReaderProvider that can generate SELECT, you can get DELETE by modifying the SELECT statement a little bit! 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: transform sql statement return com.ExecuteNonQuery ();}}
Here, take the query generated by where directly to GetCommand, and the resulting sql statement is roughly as follows:
SELECT fields... FROM tableName AS TableAlias WHERE Condition
Target for LINQ to SQL deletion:
DELETE FROM tableName WHERE Condition
It can be seen that the key is to get the tableName, using the regular is * *. However, there is another drawback here, which is that you can only use expression for deletion, not linq query, for example, I want to:
Var query = from item in context.Items where item.Name.StartsWith ("XX") select item; context.DeleteAll (query)
It seems that you want to put DeleteAll into DataContext, but there is a risk that you may accept SELECT statements that cannot be converted, and it is necessary to increase judgment.
The deletion of LINQ to SQL is completed 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: reg expression is taken from MSDN Forum
This is the answer to the sample analysis question about the LINQ to SQL deletion implementation. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.