简体   繁体   English

将完整列从一个数据表移至另一C#

[英]Move complete column from one Data Table to other C#

I need to implement column shifting functionality between two data tables. 我需要在两个数据表之间实现列移动功能。 Let say i have columns {A,B,C} in DataTable 1 and {A} in DataTable 2. 假设我在数据表1中有{A,B,C}列,在数据表2中有{A}列。

If i want to move columns {B,C} to DataTable 2, how can i do this ? 如果我想将列{B,C}移到DataTable 2,该怎么做? The data in both tables should similar. 两个表中的数据应相似。

If DataTable is not a right option , then please help me on how we can achieve this .. 如果DataTable不是正确的选择,那么请帮助我如何实现此目标。

Can this be done with List> - Nested Lists ? 可以使用List>-Nested Lists完成吗? Tried below code , but thought merge isn't the right option. 在下面的代码中尝试过,但认为合并不是正确的选择。

  private void move(DataTable source, DataTable dest, string colname)
    {
        var result = source.AsEnumerable().Select(row => row.Field<string>(colname));
        dest.Columns.Add(colname);
        DataTable dt = source.DefaultView.ToTable(false, colname);

        dest.Merge(dt);          

    }

I'm beginner , so please suggest if there is any other way where we can shift columns based on the user selection. 我是初学者,所以请提出是否还有其他方法可以根据用户选择来移动列。

检查数据表是否有复制选项,例如DataTable dt = new DataTable()dt.copy或dt.clone

Sometime back I had done this. 有时我已经做到了。 I had to manually write the code for data table copy. 我必须手动编写用于数据表复制的代码。 The method below does the job of copying missing columns from the source table to the target table. 下面的方法完成将丢失的列从源表复制到目标表的工作。 It returns the collection of column names that got copied. 它返回被复制的列名的集合。 The second function copies the data from source column to the target column. 第二个函数将数据从源列复制到目标列。 It takes the primary key column name as input and list of column names for which data has to be copied. 它以主键列名作为输入,并以列名列表为基础,必须为其复制数据。

public List<string> CopyDistinctColumns(DataTable source, DataTable target)
{
    var copiedColumn = new List<string>();
    for(var x = 0; x < source.Columns.Count; x++)
    {
        var column = source.Columns[x];
        if(!target.Columns.Contains(column.ColumnName))
        {
            var newCol = new DataColumn
            {
                ColumnName = column.ColumnName,
                DataType = column.DataType,
                Caption = column.Caption,
                DefaultValue = column.DefaultValue,
                MaxLength = column.MaxLength,
            };
            copiedColumn.Add(column.ColumnName);
            target.Columns.Add(newCol);
        }
    }
    return copiedColumn;
}

public void CopyData(DataTable source, DataTable target, string primaryKeyColumnName, List<string> copiedColumns)
{
    for (var i = 0; i < source.Rows.Count; i++)
    {
        var row = source.Rows[i];
        var primaryKeyValue = row[primaryKeyColumnName];
        for (var j = 0; j < target.Rows.Count; j++)
        {
            var trow = target.Rows[j];
            if (trow[primaryKeyColumnName].Equals(primaryKeyValue))
            {
                foreach (var col in copiedColumns)
                {
                    trow[col] = row[col];
                }
                break;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM