c# - 将数据表列附加到其他数据表
问题描述
我有两个数据表。第一个有多行具有相同的“IPC”值但不同的“Subordination”值。像这样: 在此处输入图像描述
第二个数据表具有“IPC”(BBG IPC 代码)和“Subordination”(高级)的唯一值,如下所示: 在此处输入图像描述
我需要将第一个数据表的所有列与匹配的 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");
}
}
}
}
推荐阅读
- python - 如何在 if 语句中更改单元格的内容?
- java - 具有两个版本 protobuf 的 maven 多模块项目
- json - Haskell - 如何将基于管道的 JSON 转换为数据记录?
- android - 在 github 操作上找不到 Android ndk 文件
- sql - 获取 Slick 中的最后一行
- react-native - undefined 不是对象(评估“prevdeps.length”)
- node.js - 打开端口后出现错误:错误 [ERR_STREAM_WRITE_AFTER_END]: write after end (node.js)
- spring - 春季 4 中带有 log4j2 和 ms sqlserver 的 log4jdbc-log4j2 无法正常工作
- php - 如何组合到chartjs宽度php的数组
- security - nextjs 会自动清理 post 请求和请求参数吗?