将数据表列追加到其他数据表

时间:2020-07-31 09:23:03

标签: c# linq datatable datatables

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

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

我需要将具有匹配的IPC和Subordination的第一个数据表的所有列追加到第二个数据表中(IPC和Subordination列已经具有不同的列名)。

赞: enter image description here

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

1 个答案:

答案 0 :(得分:-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");

            }

        }
    }
}