简体   繁体   English

当记录与使用linq传递的值不相等时如何删除记录

[英]How to delete the records when it is not equal with the values that are passed using linq

I want to delete the records that are not there in database. 我想删除数据库中不存在的记录。 I will take an example. 我将举一个例子。 I am sending 3 questions to the controller. 我正在向控制器发送3个问题。 Q1,Q3 and Q4. Q1,Q3和Q4。 In the database there are only two records Q1 and Q2. 在数据库中,只有两个记录Q1和Q2。 Then i need to delete the Q2 and i need to add Q3 and Q4. 然后我需要删除Q2,我需要添加Q3和Q4。 How can i write a query using linq for this. 我该如何使用linq编写查询。 Hope you understand my question. 希望你理解我的问题。

Regards, Sri 问候,斯里

I don't know much about it but if it isn't a large amount of data you can try something like this: 我对此了解不多,但是如果不是大量数据,则可以尝试如下操作:

List<object> inserted = null; // Change null for all the elements in the table.
IEnumerable<object> diff = newItems.Except(inserted); // Assume newItems is the list with new items.
foreach(object elem in diff)
{
    if (newItems.Contains(elem))
        // insert
    else
        // delete
}

Have in mind that comparing objects (instead of base types) wont work. 请记住,比较对象(而不是基本类型)是行不通的。 You will have to create an IEqualityComparer as shown here . 你必须创建一个的IEqualityComparer如图所示这里

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

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