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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:52:59+00:00 2026-06-10T12:52:59+00:00

I have N datatables, where N-1 datatables are representing some entities and 1 representing

  • 0

I have N datatables, where N-1 datatables are representing some entities and 1 representing relationship between these entities.

like
Entity Country

Country DATATABLE

ID    | Country Name | Country Code
------------------------------------
ID1   | USA          | USA
ID2   | INDIA        | IND
ID3   | CHINA        | CHI

Entity Continent

Continent DATATABLE

ID     | Continent Name | Continent Code
------------------------------------
IDC1   | NORTH AMERICA  | NA
IDC2   | SOUTH AMERICA  | SA
IDC3   | ASIA           | AS

Entity Company

Company DATATABLE

ID    | Company Name   | Company Code
------------------------------------
CM1   | XYZ Company    | XYZ
CM2   | Fun Company    | Fun
CM3   | ABC Company    | ABC

Relationship among these.

Company_Country_Continent_Relationship DataTable 

ID    | Company        | Country    |  Continent     | Some Value1     | Some Value 2
-------------------------------------------------------------------------------------
R1    | CM1            | ID1        |  IDC1          | 100             | 150
R2    | CM2            | ID2        |  IDC3          | 200             | 200
R3    | CM3            | ID1        |  IDC1          | 150             | 250
R4    | CM1            | ID3        |  IDC3          | 100             | 150
R5    | CM2            | ID1        |  IDC1          | 200             | 200
R6    | CM3            | ID2        |  IDC3          | 150             | 250
R7    | CM1            | ID2        |  IDC3          | 100             | 150
R8    | CM2            | ID3        |  IDC3          | 200             | 200
R9    | CM3            | ID3        |  IDC3          | 150             | 250

Now I need to generate another relationship table, which will hold Name instead of ID.
In this example relationship data is storing ID for company, country and continent, now I want to transform these id value to there name i.e . instead or CM1 – XYZ Company.

For this transformation, I am using a method TramnsformRelationshipData, and it is working correctly.

    public static DataTable TramnsformRelationshipData(DataTable relationshipData, Dictionary<string, DataTable> mapping)
    {
        DataTable transformedDataTable = null;
        if (relationshipData == null || mapping == null )
           return null;

        transformedDataTable = relationshipData.Copy();

        foreach (DataColumn item in relationshipData.Columns)
        {
            if (mapping.ContainsKey(item.ColumnName))
            {
                var instanceData = mapping[item.ColumnName];
                if (instanceData == null)
                    return null;

                foreach (DataRow row in transformedDataTable.Rows)
                {
                    var filteredRows = instanceData.Select("ID = '" + row[item.ColumnName] + "'");
                    if (filteredRows.Any())
                        row[item.ColumnName] = filteredRows[0][1];
                }
            }
        }

        return transformedDataTable;
    }

But, this method iterating all the datatables, and being very slow, when relationshipdata having more entities to transform. So, How can i optimize this code, to work a large number of datatables with large number of rows.

Edited : In most conditions, These data are not stored in DB, These are in memory, and in memory the count for these datatables can be increased or decreased.

Thanks.

  • 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-06-10T12:53:00+00:00Added an answer on June 10, 2026 at 12:53 pm

    The solution here is to create a hash based collection (i.e. hashtables,dictionary,lookups in .NET)
    with the ID column being the key and using that instead of the .Select(Id = x)

    Code could look something like this…. Untested.

    public static DataTable TramnsformRelationshipData(DataTable relationshipData, Dictionary<string, DataTable> mapping) 
        { 
            Dictionary<string,Dictionary<string,DataRow>> newMappings = new Dictionary<string,Dictionary<string,DataRow>>();
            foreach (var kvp in mapping)
            {
                newMappings.Add(kvp.Key,kvp.Value.Rows.Cast<DataRow>().ToDictionary(dr=>dr["ID"] as string));
            }
    
            DataTable transformedDataTable = null; 
            if (relationshipData == null || mapping == null ) 
               return null; 
    
            transformedDataTable = relationshipData.Copy(); 
    
            foreach (DataColumn item in relationshipData.Columns) 
            { 
                if (newMapping.ContainsKey(item.ColumnName)) 
                { 
                    var instanceData = newMapping[item.ColumnName]; 
                    if (instanceData == null) 
                        return null; 
    
                    foreach (DataRow row in transformedDataTable.Rows) 
                    { 
                    //  var filteredRows = instanceData.Select("ID = '" + row[item.ColumnName] + "'"); 
                    //  if (filteredRows.Any()) 
                        row[item.ColumnName] = instanceData[row[item.ColumnName]][1];                       
                    } 
                } 
            } 
    
            return transformedDataTable; 
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two DataTables. First is DataTable NameAddressPhones = new DataTable(); with Three columns
I'd like to have a dataset or datatables, and be able to run SQL
we have 2 DataTables in a .NET application having a Client / Parent relationship
I have two DataTables: dt1 - personid, name dt2 - personid I want to
I have like below: After I have changed show entries to 25 in datatable,
I have a jquery datatable using the following code: // Test list table $('#dashboard_testlist_table').dataTable({
I have two datatables. 1st table-----> DataTable _dtMain = new COrder().GetDetails(); 2nd table-----> DataTable
I'm using DataTables and have this code to highlight the rows selected: /* Click
Q: If I have two DataTables like this : Dt1 (emp_num,emp_name,type) Dt2 (emp_num,emp_name,type) I
I have some DataTables loaded into memory from various sources (SQL and MS Access),

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.