简体   繁体   English

如何使用LINQ-to-SQL更新表而不必删除所有现有记录?

[英]How do I update a table with LINQ-to-SQL without having to delete all the existing records?

Lets say I have a simple table that only contains two columns: 假设我有一个只包含两列的简单表:

MailingListUser MailingListUser
- PK ID (int) - PK ID (int)
- FK UserID (int) - FK UserID (int)

I have a method called UpdateMailList(IEnumerable <int > userIDs) . 我有一个名为UpdateMailList(IEnumerable <int > userIDs)的方法UpdateMailList(IEnumerable <int > userIDs)

How do I, in LINQ, make inserts for the userIDs that are present in the passed in parameter but don't exist in the db, delete the ones that are in the db but no longer in UserIDs, and leave the ones that are already in both the db and userIDs alone? 在LINQ中,如何为传入的参数中存在但在db中不存在的userID进行插入,删除db中但不再存在于UserID中的那些,并保留已经存在的那些仅在db和userID中?

A user is presented with a checkbox list, and when it first loads it has the existing members selected for the maillist checked. 向用户显示一个复选框列表,当它第一次加载时,它会为已选中的maillist选择现有成员。

The user can then check and uncheck various users and hit "save". 然后,用户可以检查并取消选中各种用户并点击“保存”。 Once this happens, I need to update the state of the database with the state of the checklist. 一旦发生这种情况,我需要使用清单的状态更新数据库的状态。

Here is what I'm doing now: 这是我现在正在做的事情:

        public void UpdateMailList(IEnumerable<int> userIDs)
        {
            using (MainDataContext db = new MainDataContext())
            {
                var existingUsers = (from a in db.MailListUsers
                                     select a);

                db.MailListUsers.DeleteAllOnSubmit(existingUsers);
                db.SubmitChanges();


                var newUsers = (from n in userIDs
                                select new MailListUser
                                {
                                    UserID = n
                                });

                db.MailListUsers.InsertAllOnSubmit(newUsers);
                db.SubmitChanges();
            }
        }
    }
}

Is there a better way than simply deleting all entries in the MailingListUser table, and re-inserting all the userIDs values? 有没有比简单地删除MailingListUser表中的所有条目并重新插入所有userIDs值更好的方法?

Something like this should work: 这样的事情应该有效:

var existingUsers = from u in db.MailListUsers
                    select u;

var deletedUsers = from u in existingUsers
                   where !userIDs.Contains(u.UserID)
                   select u;

var newUsers = from n in userIDs
               let ids = from u in existingUsers
                         select u.UserID
               where !ids.Contains(n)
               select new MailListUser {
                    UserID = n
               };

db.MailListUsers.DeleteAllOnSubmit(deletedUsers);
db.MailListUsers.InsertAllOnSubmit(newUsers);
db.SubmitChanges();

To query users that need deleted, do: 要查询需要删除的用户,请执行:

var delUsers = from u in db.MailListUsers where !userKeys.Contains(u.UserKey) select u; var delUsers = from db in db.MailListUsers where!userKeys.Contains(u.UserKey)select u;

db.MailListUsers.DeleteAllOnSubmit(delUsers); db.MailListUsers.DeleteAllOnSubmit(delUsers);

I'm not sure the best way to do the new records. 我不确定做新记录的最佳方法。 You could do this, but I'm not sure that's the most efficient: 你可以做到这一点,但我不确定这是最有效的:

var newUserKeys = from u in userKeys where (db.MailListUsers.Where(j => j.UserKey == u.UserKey).Count() == 0) select u; var newUserKeys = from user in userKeys where(db.MailListUsers.Where(j => j.UserKey == u.UserKey).Count()== 0)select u;

I'm not 100% sure that would work; 我不是百分百肯定会起作用; alternatively, you could select all of the existing user keys, and then cross reference against this: 或者,您可以选择所有现有用户密钥,然后对此进行交叉引用:

var newUserKeys = userKeys.Where(i => !existingKeys.Contains(i.UserKey)); var newUserKeys = userKeys.Where(i =>!existingKeys.Contains(i.UserKey));

Again, don't know all the performance implications. 同样,不知道所有性能影响。

HTH. HTH。

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

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