Link to home
Start Free TrialLog in
Avatar of Gani tpt
Gani tpt

asked on

C# Or LINQ - how to merge two data sheets with different columns in c#

Hi,

I want to merge two data sheets with different columns in c#..

For example,

Based on my sheets and column mention, it should merge the data and display in final output as datatable.

Sheet1 ==> columnA data and columnC data

sheet3 ==> columnD data and Column F data

datatable result ==> merge of ColumnA & ColumnC as one column

                               merge of ColumnD & ColumnF as another column

my input string is sheet and column.

for example,

inPut string Sheets ==> "Sheet1" and "Sheet3"

Input string Columns ==> ColumnA & ColumnC(Sheet1)

                                       ColumnD & ColumnF(Sheet3)...

How to form code in either LINQ or C#..?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gani tpt
Gani tpt

ASKER

working...
Just for completeness, you could also perform a Method Based Join:
var people = names.Join(demographics, 
                        name => name.ID, 
                        demographic => demographic.ID, 
                        (name, demographic) => new { name.ID, name.FirstName, name.LastName, demographic.Birthdate, demographic.IsWorking }).ConvertToDataTable();

Open in new window

Or an Expression Based Join:
var people = (from name in names
              join demographic in demographics
              on name.ID equals demographic.ID
              select new { name.ID, name.FirstName, name.LastName, demographic.Birthdate, demographic.IsWorking }).ConvertToDataTable();

Open in new window


-saige-