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

  • Home
  • SEARCH
  • 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 1053177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:12:43+00:00 2026-05-16T17:12:43+00:00

I’m trying to LINQ two tables based on a dynamic key. User can change

  • 0

I’m trying to LINQ two tables based on a dynamic key. User can change key via a combo box. Key may be money, string, double, int, etc. Currently I’m getting the data just fine, but without filtering out the doubles. I can filter the double in VB, but it’s slooooow. I’d like to do it in the LINQ query right out of the gate.

Here’s the data:

First Table:

 -------------------------------------------------------------
| AppleIndex  | AppleCost  | AppleColor  | AppleDescription   |
 ------------------------------------------------------------
|     1       |     3      | Red         | This is an apple   |
|     2       |     5      | Green       | This is an apple   |
|     3       |     4      | Pink        | This is an apple   |
|     4       |     2      | Yellow      | This is an apple   |
|     5       |     2      | Orange      | This is an apple   |
|     1       |     3      | Red         | This is a duplicate|
|     2       |     5      | Green       | This is a duplicate|
|     3       |     4      | Pink        | This is a duplicate|
|     4       |     2      | Yellow      | This is a duplicate|
|     5       |     2      | Orange      | This is a duplicate|
 -------------------------------------------------------------

Second Table:

 ------------------------------------------------------------
| OrangeIndex | OrangeCost | OrangeColor | OrangeDescription |
 ------------------------------------------------------------
|     1       |     1      | Orange      | This is an Orange |
|     2       |     3      | Orange      |                   |
|     3       |     2      | Orange      | This is an Orange |
|     4       |     3      | Orange      |                   |
|     5       |     2      | Orange      | This is an Orange |
 ------------------------------------------------------------

Currently, I’m using the following code to get too much data:

Dim Matches = From mRows In LinqMasterTable Join sRows In LinqSecondTable _
              On mRows(ThePrimaryKey) Equals sRows(TheForignKey) _
              Order By mRows(ThePrimaryKey) _
              Select mRows, sRows Distinct

Outcome:

 -------------------------------------------------------------------------
| 1  | 3 | Red    | This is an apple     | 1 | Orange | This is an Orange |
| 1  | 3 | Red    | This is an duplicate | 1 | Orange | This is an Orange |
| 2  | 5 | Green  | This is an apple     | 3 | Orange |                   |
| 2  | 5 | Green  | This is an duplicate | 3 | Orange |                   |
| 3  | 4 | Pink   | This is an apple     | 2 | Orange | This is an Orange |
| 3  | 4 | Pink   | This is an duplicate | 2 | Orange | This is an Orange |
| 4  | 2 | Yellow | This is an apple     | 3 | Orange |                   |
| 4  | 2 | Yellow | This is an duplicate | 3 | Orange |                   |
| 5  | 2 | Orange | This is an apple     | 2 | Orange | This is an Orange |
| 5  | 2 | Orange | This is an duplicate | 2 | Orange | This is an Orange |
 -------------------------------------------------------------------------

Desired Outcome:

 ------------------------------------------------------------------------
| 1 | 3 | Red    | This is an apple | 1 | 1 | Orange | This is an Orange |
| 2 | 5 | Green  | This is an apple | 2 | 3 | Orange |                   |
| 3 | 4 | Pink   | This is an apple | 3 | 2 | Orange | This is an Orange |
| 4 | 2 | Yellow | This is an apple | 4 | 3 | Orange |                   |
| 5 | 2 | Orange | This is an apple | 5 | 2 | Orange | This is an Orange |
 ------------------------------------------------------------------------

I have tried the following:

'Get the original Column Names into an Array List
'MasterTableColumns = GetColumns(qMasterDS, TheMasterTable) '(external code)

'Plug the Existing DataSet into a DataView:
Dim View As DataView = New DataView(qMasterTable)

'Sort by the Primary Key:
View.Sort = ThePrimaryKey

'Build a new table listing only one column:
Dim newListTable As DataTable = _
View.ToTable("UniqueData", True, ThePrimaryKey)

This returns a unique list, but no associated data:

 -------------
| AppleIndex  |
 -------------
|     1       | 
|     2       | 
|     3       |
|     4       |
|     5       |
 -------------

So I tried this instead:

'Build a new table with ALL the columns:
Dim newFullTable As DataTable = _
View.ToTable("UniqueData", True, _
     MasterTableColumns(0), _
     MasterTableColumns(1), _
     MasterTableColumns(2), _
     MasterTableColumns(3))

Unfortunately, it yields the following… with duplicates:

 -------------------------------------------------------------
| AppleIndex  | AppleCost  | AppleColor  | AppleDescription   |
 ------------------------------------------------------------
|     1       |     3      | Red         | This is an apple   |
|     2       |     5      | Green       | This is an apple   |
|     3       |     4      | Pink        | This is an apple   |
|     4       |     2      | Yellow      | This is an apple   |
|     5       |     2      | Orange      | This is an apple   |
|     1       |     3      | Red         | This is a duplicate|
|     2       |     5      | Green       | This is a duplicate|
|     3       |     4      | Pink        | This is a duplicate|
|     4       |     2      | Yellow      | This is a duplicate|
|     5       |     2      | Orange      | This is a duplicate|
 -------------------------------------------------------------

Any ideas?

~~~~~~~~~~~~ Update: ~~~~~~~~~~~~

Jeff M suggested the following code. (Thanks Jeff) However, it gives me a error. Does anyone know the syntax for making this work in VB? I’ve monkeyed with it a bit and can’t seem to get it right.

Dim matches = _
    From mRows In (From row In LinqMasterTable _
        Group row By row(ThePrimaryKey) Into g() _
        Select g.First()) _
    Join sRows In LinqSecondTable _
    On mRows(ThePrimaryKey) Equals sRows(TheForignKey) _
    Order By mRows(ThePrimaryKey) _
    Select mRows, sRows

Error in Third row at “row(ThePrimaryKey)”:

“Range variable name can be inferred only from a simple or qualified name with no arguments.”

  • 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-16T17:12:43+00:00Added an answer on May 16, 2026 at 5:12 pm

    Declarations and Such:

    Private Sub LinqTwoTableInnerJoin(ByRef qMasterDS As DataSet, _
                                      ByRef qMasterTable As DataTable, _
                                      ByRef qSecondDS As DataSet, _
                                      ByRef qSecondTable As DataTable, _
                                      ByRef qPrimaryKey As String, _
                                      ByRef qForignKey As String, _
                                      ByVal qResultsName As String)
    
    Dim TheMasterTable As String = qMasterTable.TableName
    Dim TheSecondTable As String = qSecondTable.TableName
    Dim ThePrimaryKey As String = qPrimaryKey
    Dim TheForignKey As String = qForignKey
    Dim TheNewForignKey As String = ""
    
    MasterTableColumns = GetColumns(qMasterDS, TheMasterTable)
    SecondTableColumns = GetColumns(qSecondDS, TheSecondTable)
    
    Dim mColumnCount As Integer = MasterTableColumns.Count
    Dim sColumnCount As Integer = SecondTableColumns.Count
    
    Dim ColumnCount As Integer = mColumnCount + sColumnCount
    
    Dim LinqMasterTable = qMasterDS.Tables(TheMasterTable).AsEnumerable
    Dim LinqSecondTable = qSecondDS.Tables(TheSecondTable).AsEnumerable
    

    Get the Data and order it by the Selected Key:

    Dim Matches = From mRows In LinqMasterTable Join sRows In LinqSecondTable _
                 On mRows(ThePrimaryKey) Equals sRows(TheForignKey) _
                 Order By mRows(ThePrimaryKey) _
                 Select mRows, sRows
    

    Put the Results into a Dataset Table:

    ' Make sure the dataset is available and/or cleared:
    If dsResults.Tables(qResultsName) Is Nothing Then dsResults.Tables.Add(qResultsName)
    dsResults.Tables(qResultsName).Clear() : dsResults.Tables(qResultsName).Columns.Clear()
    
    'Adds Master Table Column Names
    For x = 0 To MasterTableColumns.Count - 1
        dsResults.Tables(qResultsName).Columns.Add(MasterTableColumns(x))
    Next
    
    'Rename Second Table Names if Needed:
    For x = 0 To SecondTableColumns.Count - 1
        With dsResults.Tables(qResultsName)
            For y = 0 To .Columns.Count - 1
                If SecondTableColumns(x) = .Columns(y).ColumnName Then
                    SecondTableColumns(x) = SecondTableColumns(x) & "_2"
                End If
            Next
        End With
    Next
    
    'Make sure that the Forign Key is a Unique Value
    If ForignKey1 = PrimaryKey Then
        TheNewForignKey = ForignKey1 & "_2"
    Else
        TheNewForignKey = ForignKey1
    End If
    
    'Adds Second Table Column Names
    For x = 0 To SecondTableColumns.Count - 1 
        dsResults.Tables(qResultsName).Columns.Add(SecondTableColumns(x))
    Next
    
    'Copy Results into the Dataset:
    For Each Match In Matches
    
        'Build an array for each row:
        Dim NewRow(ColumnCount - 1) As Object
    
        'Add the mRow Items:
        For x = 0 To MasterTableColumns.Count - 1
            NewRow(x) = Match.mRows.Item(x)
        Next
    
        'Add the srow Items:
        For x = 0 To SecondTableColumns.Count - 1
            Dim y As Integer = x + (MasterTableColumns.Count)
            NewRow(y) = Match.sRows.Item(x)
        Next
    
        'Add the array to dsResults as a Row:
        dsResults.Tables(qResultsName).Rows.Add(NewRow)
    
    Next
    

    Give the user an option to clean doubles or not:

    If chkUnique.Checked = True Then
        ReMoveDuplicates(dsResults.Tables(qResultsName), ThePrimaryKey)
    End If
    

    Remove the Duplicates if they so desire:

    Private Sub ReMoveDuplicates(ByRef SkipTable As DataTable, _
                             ByRef TableKey As String)
    
        'Make sure that there's data to work with:
        If SkipTable Is Nothing Then Exit Sub
        If TableKey Is Nothing Then Exit Sub
    
        'Create an ArrayList of rows to delete:
        Dim DeleteRows As New ArrayList()
    
        'Fill the Array with Row Number of the items equal 
        'to the item above them:
        For x = 1 To SkipTable.Rows.Count - 1
            Dim RowOne As DataRow = SkipTable.Rows(x - 1)
            Dim RowTwo As DataRow = SkipTable.Rows(x)
            If RowTwo.Item(TableKey) = RowOne.Item(TableKey) Then
                DeleteRows.Add(x)
            End If
        Next
    
        'If there are no hits, exit this sub:
        If DeleteRows.Count < 1 Or DeleteRows Is Nothing Then
            Exit Sub
        End If
    
        'Otherwise, remove the rows based on the row count value:
        For x = 0 To DeleteRows.Count - 1
    
            'Start at the END and count backwards so the duplicate 
            'item's row count value doesn't change with each deleted row
            Dim KillRow As Integer = DeleteRows((DeleteRows.Count - 1) - x)
    
            'Delete the row:
            SkipTable.Rows(KillRow).Delete()
    
        Next
    End Sub
    

    Then clean up any leftovers:

    If Not chkRetainKeys.Checked = True Then 'Removes Forign Key
        dsResults.Tables(qResultsName).Columns.Remove(TheNewForignKey)
    End If
    
    'Clear Arrays
    MasterTableColumns.Clear()
    SecondTableColumns.Clear()
    

    Final Analysis:
    Ran this against 2 Files with 4 columns, 65,535 rows, and with some doubles. Process time, roughly 1 second. In fact it took longer to load the fields into memory than it did to parse the data.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.