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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:24:52+00:00 2026-06-03T21:24:52+00:00

I have two table where two column are fixed. Some columns are identical and

  • 0

I have two table where two column are fixed. Some columns are identical and some are new.Columns are dynamic.

Have to do it in code level and I am trying to loop and conditions

What I want is to generate a report following the condition,

  • All columns in table1 and table2 must be present.
  • If a column is common and value is there it should be added with the identical row in other table.
  • If any row is present in one table but not in other, it should be included.

Example data

Table1

ID  | NAME | P1  | P2  | P3 
 ----------------------------
  1  | A1   | 1   |  2 | 3.3
  2  | A2   | 4.4 |  5 |  6

TABLE 2

  ID  | NAME | P1 | P2  | P4 
 ---------------------------
  1  | A1   | 10  | 11  |  12
  2  | A2   | 12  | 14  |  15
  3  | A3   | 16  | 17  |  18

Expected output:

 ID  | NAME | P1   |  P2  | P3  | P4
 ---------------------------------
  1  | A1   | 11   |  13  | 3.3 | 12
  2  | A2   | 16.4 |  19  |  6  | 15
  3  | A3   | 16   |  17  | null| 18

Progress till now:

First I merged those two table in to table1
table1.Merge(table2)

Then trying to group by over it

var query = from row in table1.AsEnumerable()
                        group row by new
                        {
                            ID = row.Field<int>("ID"),
                            Name = row.Field<string>("Name")
                        }
                            into grp
                            select new
                            {
                                ID = grp.Key.ID,
                                Name = grp.Key.Name,
                                Phase1 = grp.Sum(r => r.Field<decimal>("P1"))
                            };

I have modified this code to get a datatable. Please see attached cs file.
This is working, but as the number of columns are dynamic, I guess I have to repeat it for other columns and join all these small tables where one columns will be added.

How can I merge all those small tables?

I am lost here.Is there any other way. Its feeling as stupid thing.

Any help would be appreciated.

Attached File:


http://dl.dropbox.com/u/26252340/Program.cs

  • 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-03T21:24:53+00:00Added an answer on June 3, 2026 at 9:24 pm

    Got this solved by

    table1.Merge(table2, true, MissingSchemaAction.Add);
    finalTable = table1.Clone();
    finalTable.PrimaryKey = new DataColumn[] { finalTable.Columns["ID"], finalTable.Columns["Name"] };
    List<string> columnNames = new List<string>();
    for (int colIndex = 2; colIndex < finalTable.Columns.Count; colIndex++)
    {
    columnNames.Add(finalTable.Columns[colIndex].ColumnName);
    }
    foreach (string cols in columnNames)
    {
    var temTable = new DataTable();
    temTable.Columns.Add("ID", typeof(int));
    temTable.Columns.Add("Name", typeof(string));
    temTable.Columns.Add(cols, typeof(decimal));
    
    (from row in table1.AsEnumerable()
    group row by new { ID = row.Field<int>("ID"), Team = row.Field<string>("Team") } into grp
    orderby grp.Key.ID
    select new
    {
    ID = grp.Key.ID,
    Name = grp.Key.Team,
    cols = grp.Sum(r =>  r.Field<decimal?>(cols)),
    })
    .Aggregate(temTable, (dt, r) => { dt.Rows.Add(r.ID, r.Team, r.cols); return dt; });
    
    finalTable.Merge(temTable, false, MissingSchemaAction.Ignore);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with similar columns - let's say table A with column
I have a table with two columns: column A column B 1 2 1
I have two tables. One table is a fixed column(ie. the table will not
I have two tables Table public.tags_to_entities Column | Type | Modifiers --------+---------+----------- tid |
I have TWO tables 1) column 2) row COLUMN TABLE COLumnID ---------------- 1 2
I have an application in which I'm currently using a two column table in
I have two tables; A and B. Table A contains a column of GUIDs.
I have a two-column primary key on a table. I have attempted to alter
I have a table with a column event_time . How can I select two
I have a table that is accessed by row and column, where those two

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.