首页 > 解决方案 > 将数据表列附加到其他数据表

问题描述

我有两个数据表。第一个有多行具有相同的“IPC”值但不同的“Subordination”值。像这样: 在此处输入图像描述

第二个数据表具有“IPC”(BBG IPC 代码)和“Subordination”(高级)的唯一值,如下所示: 在此处输入图像描述

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

像这样: 在此处输入图像描述

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

标签: c#linqdatatabledatatables

解决方案


尝试以下:

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");

            }

        }
    }
}

推荐阅读