C# 减去两个数据表值
我有两个具有相同模式的数据表。我想从2个数据表中减去这些值。 请注意,我使用pivot查询从数据库中获取datatable。 我正在为我的问题寻找最好的解决办法 例如,我有以下数据表C# 减去两个数据表值,c#,sql,pivot,C#,Sql,Pivot,我有两个具有相同模式的数据表。我想从2个数据表中减去这些值。 请注意,我使用pivot查询从数据库中获取datatable。 我正在为我的问题寻找最好的解决办法 例如,我有以下数据表 Datatable 1 Date Amount1 Amount2 Amount3 01/01/2019 45 21 21 02/01/2019 55 23 23 03/01/2019 458 3 23 04/01/2019 43 3 4 05/01/2019 534 45 67
Datatable 1
Date Amount1 Amount2 Amount3
01/01/2019 45 21 21
02/01/2019 55 23 23
03/01/2019 458 3 23
04/01/2019 43 3 4
05/01/2019 534 45 67
06/01/2019 34 67 78
07/01/2019 45 7 56
08/01/2019 456 78 6
Datatable 2
Date Amount1 Amount2 Amount3
01/01/2019 52 2 23
02/01/2019 65 3 23
03/01/2019 7 3 23
04/01/2019 52 2 3
05/01/2019 42 45 67
06/01/2019 54 3 78
07/01/2019 45 7 234
08/01/2019 54 3 24
我的预期输出应该是
Date Amount1 Amount2 Amount3
01/01/2019 -7 19 -2
02/01/2019 -10 20 0
03/01/2019 451 0 0
04/01/2019 -9 1 1
05/01/2019 492 0 0
06/01/2019 -20 64 0
07/01/2019 0 0 -178
08/01/2019 402 75 -18
转换成列表后。您需要将列表转换为DataTable。我正在编写将datatable转换为list的代码。将此result1变量传递给ToDataTable方法
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
我已经附上了一张图片,我知道是你想要减去的表格和最终结果。您是否考虑过在数据库上创建视图并只查询结果的可能性?我在下面留下了一些SQL代码SQL Server 2012,您可以使用它们来取消源表的pivot,获取结果并再次透视表。您的源表是p1和p2 选择日期,[dateamount1]作为dateamount1,[dateamount2]作为dateamount2,[dateamount3]作为dateamount3 从…起 选择unP1.date作为日期,unP1.Concept作为概念,unP1.orders-unp2.orders作为起始日期 选择日期、概念、订单 从…起 选择日期、dateamount1、dateamount2、dateamount3 从dbo.p1到P1u UNPIVOT 概念车订单 dateamount1、dateamount2、dateamount3 作为uP1作为unP1 内连接 选择日期、概念、订单 从…起 选择日期、dateamount1、dateamount2、dateamount3 从dbo.p2到P1u UNPIVOT 概念车订单 dateamount1、dateamount2、dateamount3 uP2与unP2一样 在unP1.date=unp2.date和unP1.Concept=unp2.Concept p上 支点 苏米特迪夫 对于中的概念 结果为[dateamount1]、[dateamount2]、[dateamount3] 希望能有帮助
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}