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



我有两个数据表。第一个具有多行;IPC";值但不同";从属关系";价值像这样:在此处输入图像描述

第二数据表具有唯一值"0";IPC";(BBG IPC代码(和";从属关系";(资历(是这样的:在此处输入图像描述

我需要在第二个数据表中添加第一个数据表的所有列,这些列具有匹配的IPC和Subordination(除了已经存在的具有不同列名的IPC和Subordination列(。

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

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

尝试以下操作:

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

最新更新