开发者

Removing non-unique data on insert

开发者 https://www.devze.com 2023-01-17 02:55 出处:网络
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 dupl

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消