简体   繁体   中英

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. Q1,Q3 and Q4. In the database there are only two records Q1 and Q2. Then i need to delete the Q2 and i need to add Q3 and Q4. How can i write a query using linq for this. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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