views:

43

answers:

1

Table entity column is unique, and I'm trying to add items. Items amount is fairly big, so i'm trying to avoid connecting to a database many times. What is the best way to ignore duplicates?

Example: code

Edit: Problem was word1 was of type nchar(50), so after changing it to nvarchar(50) everything worked.

+2  A: 

It sounds like you want to avoid inserting those words that are already in the table.

HashSet<string> words; 

List<string> newWords = words.Except(lh.words.Select(w=>w.word1))
                             .ToList();

lh.words.InsertAllOnSubmit(newWords.Select(x=> new word { word1 = x}));

Your code, as you have it today, will send all the INSERTS one after each other, in the same batch. It'll create a single connection for them all with the one SubmitChanges() that you have.

p.campbell
have you considered perormnce of hashset for a large no of records?
saurabh
Problem is that Hashset will contain elements, already in db. And elements in HashSet are lowercased already.
Margus
@Margus: updated answer for you!
p.campbell