Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,
Please how do I insert multiple rows using entity framework and check for duplicates and remove them into another table?'

i want to do DBContext.AddRange(List<t>) and push the duplicates into another table.
Thank you.

What I have tried:

dbSet.AddRange(bindingModel);
     var result = await _Context.SaveChangesAsync();
Posted
Updated 3-Aug-20 21:13pm
v2
Comments
BillWoodruff 3-Aug-20 18:34pm    
Hi, Do you mean you want to create another Table to hold duplicates ?
Afzaal Ahmad Zeeshan 3-Aug-20 21:35pm    
Normally, Entity Framework should be able to check for a duplicate based on the primary key.

But, if you do not have the primary keys and you want to "INSERT" the records but ignore the duplicates, then the first query which records are available.

I would recommend using a stored procedure to improve performance.
Enobong Adahada 6-Aug-20 11:37am    
Yes, I have Primary key and yes I want to put the duplicates in a separate table nd save only the duplicate free records
Enobong Adahada 6-Aug-20 11:38am    
Yes, I have Primary key and yes I want to put the duplicates in a separate table and save only the duplicate free records

1 solution

You can find duplicate values by using following

List<String> duplicates = lst.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .Select(g => g.Key)
                             .ToList();
 
Share this answer
 
Comments
Enobong Adahada 6-Aug-20 11:42am    
Thus will slow down things as my table is quite large, about 15million records.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900