LINQ is a query technology, but I think we know what you mean; you might want to be a bit more specific whether this is LINQ-to-SQL or Entity Framework. You might also want to clarify what "bulk" means in your case... for 10-100 records you might use a different answer to 10,000 records (where SqlBulkCopy
into a staging table and a stored procedure to import at the db would be the best idea).
For a relatively low number - just use your ORM tool to find the records - for example with LINQ-to-SQL (perhaps with a spanning serializable transaction) - and using C# for illustration (updated to show loop and cache):
Dictionary<string,Tag> knownTags = new Dictionary<string,Tag>();
foreach(... your data ...) {
Tag tag;
if(!knownTags.TryGetValue(tagName, out tag)) {
tag = ctx.Tags.SingleOrDefault(t => t.Name == tagName);
if(tag == null) {
tag = new Tag { Name = tagName };
ctx.Tags.InsertOnSubmit(tag);
}
knownTags.Add(tagName, tag);
}
// insert video tag
}
ctx.SubmitChanges();
Actually, for performance reasons I wonder whether this might be one of those occasions where a natural-key makes sense - i.e. use Tag
(the varchar
) as the primary key, and duplicate it (as a foreign key) in VideoTags
- then you don't need to join to the Tags
table all the time.
If the numbers are larger, it is pretty easy to use SqlBulkCopy
; just put the data into a DataTable
and push it over, then do the work in TSQL.