Home >Backend Development >C++ >How Can I Efficiently Merge Multiple DataTables with Different Structures into a Single Table?

How Can I Efficiently Merge Multiple DataTables with Different Structures into a Single Table?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-01 12:20:10261browse

How Can I Efficiently Merge Multiple DataTables with Different Structures into a Single Table?

Combining Multiple DataTables into a Single Comprehensive Table

Challenge:
Merging multiple DataTables with differing column definitions and row counts while ensuring alignment and preserving data integrity can be challenging.

Solution:
To overcome this issue and create a single comprehensive DataTable, you can employ the following custom method:

public static DataTable MergeAll(this IList<DataTable> tables, String primaryKeyColumn)
{
    // Validate arguments
    if (!tables.Any())
        throw new ArgumentException("Tables must not be empty", "tables");
    if (primaryKeyColumn != null)
        foreach (DataTable t in tables)
            if (!t.Columns.Contains(primaryKeyColumn))
                throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");

    DataTable table = new DataTable("TblUnion");

    // Disable data validation during bulk loading
    table.BeginLoadData();

    // Merge all tables into the result table
    foreach (DataTable t in tables)
        table.Merge(t, false, MissingSchemaAction.Add);

    // End data validation
    table.EndLoadData();

    // Handle duplicate primary keys (if specified)
    if (primaryKeyColumn != null)
    {
        // Group rows by primary key column
        var pkGroups = table.AsEnumerable().GroupBy(r => r[primaryKeyColumn]);

        // Identify groups with duplicate keys
        var dupGroups = pkGroups.Where(g => g.Count() > 1);

        // Combine data from duplicate rows into the first row of each group
        foreach (var grpDup in dupGroups)
        {
            // Use the first row and modify it to include data from other rows
            DataRow firstRow = grpDup.First();
            foreach (DataColumn c in table.Columns)
            {
                if (firstRow.IsNull(c))
                {
                    // Find the first non-null row for the current column and copy its value
                    DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
                    if (firstNotNullRow != null)
                        firstRow[c] = firstNotNullRow[c];
                }
            }

            // Remove duplicate rows
            var rowsToRemove = grpDup.Skip(1);
            foreach (DataRow rowToRemove in rowsToRemove)
                table.Rows.Remove(rowToRemove);
        }
    }

    // Return the merged table
    return table;
}

Usage:

var tables = new[] { tblA, tblB, tblC };
DataTable TblUnion = tables.MergeAll("c1");

This method provides a robust solution for merging DataTables, handling data alignment, and preserving key data.

The above is the detailed content of How Can I Efficiently Merge Multiple DataTables with Different Structures into a Single Table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn