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 6163873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:53:53+00:00 2026-05-23T21:53:53+00:00

There’s a table Category with a pk idCategory and a self-referencing foreign-key fiCategory .

  • 0

There’s a table Category with a pk idCategory and a self-referencing foreign-key fiCategory. That means categories are “main-categories” when fiCategory is null. If fiCategory links to another category, it is a sub-category of it. But it’s also valid that this sub-category also has 1-n sub-categories(with fiCategory linking to it’s idCategory).

Q: How can i get a list of main-categories,sub-categories,”sub-sub-categories”, …etc. with LINQ?

Backgound:

I’m using typed DataSets to compare data from Server1/MySQL with data from Server2/MS SQL-Server. After normalizing and cleaning(there are several inconsistencies) i want to import the new data into SQL-Server. First of all i have to import the main-categories, then the sub-categories and so on. Otherwise SQL-Server would throw a constraint exception when i would try to insert a row with a foreign-key to a category that is yet not inserted.

These are the tables(left MySQL-source, right SQL-Server destination table):

Kategorie: MySQL-source Category: SQL-Server destination

Here i’m getting the new rows in MySQL that are not in SQL-Server:

src and dest are typed DataSets

Dim idSrc = From c In src.kategorie Select c.kategorie_id
Dim idDest = From c In dest.Category Select c.idCategory
Dim diff = idSrc.Except(idDest)
Dim needUpdate = diff.Any

Now i want to import the new rows.
In this way i get all “main-categories”:

Dim mainCat = From kat In src.kategorie
            Join d In diff
            On kat.kategorie_id Equals d
            Where kat.IsparentNull
        Select kat

For Each cat In mainCat
    Dim newCat = Me.dest.Category.NewCategoryRow
    newCat.idCategory = cat.kategorie_id
    newCat.Name = cat.name
    newCat.SetfiCategoryNull()
    dest.Category.AddCategoryRow(newCat)
    rowsUpdated += daCategoryOut.Update(dest.Category)
Next

In this way i get all sub-categories:

Dim subCat = From kat In src.kategorie
               Join d In diff
               On kat.kategorie_id Equals d
               Where Not kat.IsparentNull
          Select kat

Both LINQ-queries are working, but how do i get all “levels” of sub-categories? I need to insert the rows from “top” to “bottom”. Is there a way that works even with any depth?

At least this is not working(repeating pk-values):

Dim subCatWithChild = From cat In subCat
                      Join child In 
                     (From kat In src.kategorie Where Not kat.IsparentNull)
                      On child.parent Equals cat.kategorie_id
                   Select cat

I’m still learning LINQ and appreciating any kind of suggestions(also in C#). Thank you in advance.

Note: Maybe you know a way i can temporarily disable the foreign key contraint in SQL-Server and enable it after i inserted all rows from ADO.NET. That would be much simpler.


This is the solution, thanks to @Tridus:

Dim mainCat = From kat In src.kategorie
          Where kat.IsparentNull
      Select kat

For Each kat In mainCat
   rowsUpdated += insertCategory(kat, diff, daCategoryOut)
Next

This is the recursive function:

Private Function insertCategory(ByVal parent As CC_IN.kategorieRow, ByVal diff As IEnumerable(Of Int32), ByVal daCategoryOut As CC_OutTableAdapters.CategoryTableAdapter) As Int32
    Dim rowsInserted As Int32 = 0

    If diff.Contains(parent.kategorie_id) Then
        Dim newCat = Me.dest.Category.NewCategoryRow
        newCat.idCategory = parent.kategorie_id
        newCat.Name = parent.name
        If parent.IsparentNull Then
            newCat.fiCategory = parent.parent
        Else
            newCat.SetfiCategoryNull()
        End If
        dest.Category.AddCategoryRow(newCat)
        rowsInserted += daCategoryOut.Update(dest.Category)
    End If

    'get all childs from this parent
    Dim childs = From cat In Me.src.kategorie
               Where Not cat.IsparentNull AndAlso cat.parent = parent.kategorie_id
               Select cat
    'insert all childs for this parent
    For Each child In childs
        rowsInserted += insertCategory(child, diff, daCategoryOut)
    Next

    Return rowsInserted
End Function
  • 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-23T21:53:54+00:00Added an answer on May 23, 2026 at 9:53 pm

    Yes, Foreign Key constraints can be temporarily disabled.

    The best way to do this (other then disabling foreign keys and just copying the entire table row-by-row) is recursively starting with the main categories. Conceptually, you’d do this:

    Get the main categories (which you've done)
    For each main category
       Is this one in the other DB? If not, add it.
       Get the sub-categories of this main category.
       For each sub-category
         Is this one in the other DB? If not, add it.
         Get the sub-categories of this sub-category.
    

    etc. It’s easy to get the sub-categories of whatever category you currently have, so if you just start at the top you can walk the entire tree and add anything that’s missing on the other side.

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

Sidebar

Related Questions

There is a column that exists in 2 tables. In table 1, this column
There's a lot of reading on self referencing problems, but I can't seem to
There is a moment in my app, that I need to force to show
There are two table s : one is the master and one the detail
There are a lot of blogs saying that a hasOwnProperty check should be used
There's a thrird party app that needs to get information via custom http headers,
There's a column on one of my tables that's being updated by various INSERT/DELETE
There can only be one IDENTITY column per table Why is it so? Take
There are a lot of example implementations of daemons on the net. Most that
There are some Magento Connect extensions that I find myself installing every time I

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.