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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:25:01+00:00 2026-05-15T02:25:01+00:00

I want to perform a JOIN on two Datatables in a Dataset. For example,

  • 0

I want to perform a JOIN on two Datatables in a Dataset. For example, I am working on the AdventureWorks database. I need data from the tables [Person].[Address] and [Person].[StateProvince] and once the data is in the corresponding Datatables, I have to perform a JOIN on StateProvinceID column.

Data Structure –

Address {Address,A_StateID}
State {S_StateID,Name}

My search on the internet led me to a lot of examples which said I will have to use a Datarelation object in order to perform a JOIN. The examples I found were like –

DataColumn childcolumn = dsAdd.Tables["Address"].Columns["A_StateID"];
DataColumn parentcolumn = dsAdd.Tables["State"].Columns["S_StateID"];
DataRelation relStateID = new DataRelation("StateRel", parentcolumn, childcolumn,false);
dsAdd.Relations.Add(relStateID);

But, I have no idea what I have to do after I have added a DataRelation to perform the JOIN. So, I decided to do it myself –

    //I added a new column to my Address table obtain the State Name after performing the `JOIN`
    DataColumn A_State = new DataColumn("State");
    A_State.DataType = typeof(string);
    dsAdd.Tables["Address"].Columns.Add(A_State);

    foreach (DataRow dr in dsAdd.Tables["Address"].Rows)
    {           
        //for each row in the Address table I obtain the StateID
        string stateid = dr.ItemArray[1].ToString();
        string expression = "S_StateID =" + stateid;

        //Obtain the corresponding row from State table and update the value in new column in Address table 
        DataRow[] newdr;
        newdr = dsAdd.Tables["State"].Select(expression);
        string statename = newdr[0].ItemArray[1].ToString();
        dr.SetField("State", statename.ToString());
    }

    grdJoins.DataSource = dsAdd.Tables["Address"];
    grdJoins.DataBind();

This works fine but is there a better way to do this? How is a JOIN performed using a Datarelation object?

  • 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-15T02:25:02+00:00Added an answer on May 15, 2026 at 2:25 am

    The best way to do this is to do it on the database. Joins are relatively expensive and slow operations; databases exist as entities separate from mere collections of tables for two reasons: enforcing safe concurrent access and to allow you do joins efficiently. They use all kinds of tricks like indexing, materialized views, caching, etc, to make sure most of the heavy lifting is already done for you.

    The next best way is to use linq (to objects):

    grdJoins.DataSource = dsAdd.Tables["Address"].AsEnumerable()
                    .Join(dsAdd.Tables["State"].AsEnumerable(),
            add => add["A_StateID"], st => st["S_StateID"],
            (add, st) => new 
          { 
              Address = add["Address"],
              City = add["City"],
              StateProvince = add["StateProvince"],
              State = st["State"]
          });
    
    grdJoins.DataBind();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.