Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 259391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:15:37+00:00 2026-05-11T22:15:37+00:00

I am using VB.NET with LINQ to MS SQL. I have two following tables.

  • 0

I am using VB.NET with LINQ to MS SQL.
I have two following tables. Now I want to insert multiple items in Tags Table using LINQ, but also wants to check that if any of tag exists in Tags table. It doesn’t insert it again and bring the TagID in both cases (if inserted or if found existed)

CREATE TABLE Tags
    (TagID bigint not null , 
    Tag varchar(100) NULL )

CREATE TABLE VideoTags
    (VideoID bigint not null , 
    TagID bigint not null )

What’s the best way to acheive this using LINQ?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:15:37+00:00Added an answer on May 11, 2026 at 10:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a process in a website (Asp.net 3.5 using Linq-to-Sql for data access)
I am new to Linq and previously have been using Ado.net classes. I want
Link I'm using ASP.NET with C# and trying to use linq to sql to
Consider the following snippet: using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace ForumLogins
I'm using linq to sql for MySql (using DbLinq) in an ASP.NET MVC website.
I'm currenlty using Asp.Net Dynamic Data, with Linq to Sql, to create an simple
Overview: Using C#, I'm outputting a LINQ to SQL table of data into a
I wonder if is possible to use FTS with LINQ using .NET Framework 3.5.
I am using the (.NET 3.5 SP1) System.Xml.Linq namespace to populate an html template
I've thrown myself headfirst into C# and .Net 2.0 using Linq, and I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.