简体   繁体   English

删除条款中的NOT NOT-Linq-To-SQL

[英]Delete Where NOT in Clause - Linq-To-SQL

Currently, I am writing a MiddleWare application that synchronizes information between and accounting application (ie Quickbooks) and a SQL database. 当前,我正在编写一个MiddleWare应用程序,该应用程序可在计费应用程序(即Quickbook)和SQL数据库之间同步信息。 I am fairly new to Linq-To-SQL and am currently struggling with what originally appeared to be a simple problem. 我对Linq-To-SQL并不陌生,目前正为最初看起来很简单的问题而苦苦挣扎。

In short, I have logic that queries the chart of accounts from the accounting application (ie AR, Income, Expense, Liability, etc...). 简而言之,我具有从会计应用程序查询会计科目表的逻辑(即AR,收入,费用,负债等)。 Then, my Middleware has to determine whether or not it needs to insert, update, or delete an account in the Accounts table of the SQL database for each record. 然后,我的中间件必须确定是否需要为每个记录在SQL数据库的“帐户”表中插入,更新或删除一个帐户。 Figuring out whether or not the operation is insert or delete was pretty easy to figure out but, I am struggling with the case where an account is deleted from Quickbooks and needs to be deleted from my SQL database. 弄清楚该操作是插入还是删除是很容易弄清楚的,但是,我正在为从Quickbooks中删除帐户并需要从我的SQL数据库中删除帐户而苦苦挣扎。 I'd like to run a Linq-To-Sql query that basically deletes all records from the Account table where the ListID is not in the list of ListIDs retrieved from a query. 我想运行一个Linq-To-Sql查询,该查询基本上从Account表中删除所有列表,其中ListID不在从查询中检索到的ListID列表中。 Is there some easy way to perform this in Linq-To-Sql? 有一些简单的方法可以在Linq-To-Sql中执行此操作吗?

Assuming you've populated a list of ListIDs (listOfIDs) in a separate query: 假设您在单独的查询中填充了ListID(listOfID)列表:

var accounts = from a in db.Accounts
               where !listOfIDs.Contains(a.ListID)
               select a;
db.Accounts.RemoveAll(accounts);
db.SubmitChanges();
db.accounts.Where(x => !ListIDs.Contains(x.ID))

request it with lambda or simple linq like said before. 如前所述,请使用lambda或简单的linq请求。 then 然后

db.accounts.Attach(yourObject);
db.accounts.DeleteOnSubmit(yourObject);
db.SubmitChanges();

of course.. protect it with try/catch and welcome to Linq !!! 当然..用try / catch保护它,欢迎使用Linq !!!

You didn't say which SQL driver you're using, but whichever one it is (a pretty short list), it's certain that the driver uses the QuickBooks SDK to access to the chart of accounts. 您没有说要使用哪个SQL驱动程序,但是无论使用哪个驱动程序(很短的列表),都可以确定该驱动程序使用QuickBooks SDK来访问会计科目表。 The SDK has a special query for the purpose of finding deleted entities (such as accounts) called ListDeletedQuery . 为了查找已删除的实体(例如帐户),SDK有一个特殊的查询,称为ListDeletedQuery As a general rule, it is more efficient to use ListDeletedQuery if you want to find out what's been deleted. 通常,如果要查找已删除的内容,则使用ListDeletedQuery效率更高。 However, since the chart of accounts is usually quite small, it's probably acceptable scan the entire list in order to find missing elements as you are doing. 但是,由于会计科目表通常很小,因此可以在扫描整个列表时找到所需的缺失元素,这可能是可以接受的。 Also, ListDeletedQuery may not be exposed by the SQL driver that you're using. 另外,您使用的SQL驱动程序可能不会公开ListDeletedQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM