【问题标题】:Append data table column to other data table将数据表列附加到其他数据表
【发布时间】:2020-11-21 03:22:36
【问题描述】:

我有两个数据表。第一个有多行具有相同的“IPC”值但不同的“Subordination”值。像这样: enter image description here

第二个数据表具有唯一值“IPC”(BBG IPC 代码)和“Subordination”(高级),如下所示: enter image description here

我需要将第一个数据表的所有列与匹配的 IPC 和 Subordination 附加到第二个数据表(除了 IPC 和 Subordination 列已经存在不同的列名)。

像这样: enter image description here

我是数据表操作的初学者,我正在寻找使用 LINQ 的 C# 解决方案。谢谢你的帮助。

【问题讨论】:

    标签: c# linq datatable datatables


    【解决方案1】:

    尝试以下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    namespace ConsoleApplication11
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.Add("IPC", typeof(long));
                dt1.Columns.Add("Subordination", typeof(string));
                dt1.Columns.Add("Current SBR", typeof(string));
                dt1.Columns.Add("Forward SBR", typeof(string));
                dt1.Columns.Add("Probability", typeof(string));
                dt1.Columns.Add("Risk Category", typeof(string));
                dt1.Columns.Add("Comment", typeof(string));
                dt1.Columns.Add("Restricted Message", typeof(string));
    
                dt1.Rows.Add(new object[] { 12345, "T4", "BB", "BB", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 12345, "Senior", "BB", "BB", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 12345, "T2", "CC", "CC", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 54321, "T1", "AA", "AA", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 54321, "T2", "AA", "AA", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 12345, "T3", "BB", "BB", "High", "R1", "Something", "Something" });
                dt1.Rows.Add(new object[] { 54321, "Senior", "AAA", "AAA", "High", "R1", "Something", "Something" });
    
                DataTable dt2 = new DataTable();
                dt2.Columns.Add("Sector", typeof(string));
                dt2.Columns.Add("BBG IPC", typeof(long));
                dt2.Columns.Add("Analyst", typeof(string));
                dt2.Columns.Add("Issuer Group", typeof(string));
                dt2.Columns.Add("Seniority", typeof(string));
                dt2.Columns.Add("Mkt Value", typeof(long));
                dt2.Columns.Add("Nom Value", typeof(long));
                dt2.Columns.Add("Issue Group Rating", typeof(string));
    
                dt2.Rows.Add(new object[] { "Agency", 12345, "RIZ", "Cores", "Senior", "50256", 124515, "BB" });
                dt2.Rows.Add(new object[] { "Car", 54321, "MUS", "Cores", "T2", "515155", 15215231, "AA" });
    
                var joins = dt1.AsEnumerable().SelectMany(t1 => dt2.AsEnumerable()
                    .Where(t2 => (t1.Field<long>("IPC") == t2.Field<long>("BBG IPC")) && (t1.Field<string>("Subordination") == t2.Field<string>("Seniority")))
                    .Select(t2 => new { t1 = t1, t2 = t2 }
                )).ToList();
    
                DataTable dt3 = dt2.Clone();
                dt3.Columns.Add("Current SBR", typeof(string));
                dt3.Columns.Add("Forward SBR", typeof(string));
                dt3.Columns.Add("Probability", typeof(string));
                dt3.Columns.Add("Risk Category", typeof(string));
                dt3.Columns.Add("Comment", typeof(string));
                dt3.Columns.Add("Restricted Message", typeof(string));
    
                foreach (var join in joins)
                {
                    DataRow row = dt3.Rows.Add();
                    row.ItemArray = join.t2.ItemArray;
                    row["Current SBR"] = join.t1.Field<string>("Current SBR");
                    row["Forward SBR"] = join.t1.Field<string>("Forward SBR");
                    row["Probability"] = join.t1.Field<string>("Probability");
                    row["Risk Category"] = join.t1.Field<string>("Risk Category");
                    row["Comment"] = join.t1.Field<string>("Comment");
                    row["Restricted Message"] = join.t1.Field<string>("Restricted Message");
    
                }
    
            }
        }
    }
    

    【讨论】:

    • 我更新了代码,所以我没有向第二个原始表添加列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2020-09-05
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多