Removing non-unique data on insert
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.
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.
精彩评论