数据集关系查询与3个表和附加
本文关键字:3个 关系 查询 数据集 | 更新日期: 2023-09-27 18:31:23
我需要一些帮助从数据集中获取数据。法典:
DataRelation relation = null;
DataColumn table1Column = null;
DataColumn table2Column = null;
DataColumn table2Column1 = null;
DataColumn table3Column = null;
table1Column = tlobergeDataSet.Tb_Product.Columns[0];
table2Column = tlobergeDataSet.Tb_Transactions.Columns[2];
table2Column1 = tlobergeDataSet.Tb_Transactions.Columns[1];
table3Column = tlobergeDataSet.Tb_Consumer.Columns[0];
relation = new DataRelation("relation", table1Column, table2Column);
tlobergeDataSet.Relations.Add(relation);
relation = new DataRelation("relation1", table3Column, table2Column1);
tlobergeDataSet.Relations.Add(relation);
给定一个产品名称,我需要填充一个表格,其中包含每个不同的城市以及该产品在该城市的售价(将该产品的所有价格添加到给定城市的任何消费者)
例如表格:
产品表:
Prod_ID Name
1 tv
2 couch
消费者表:
Con_Id Name City
----------------------
1 Gray New York
2 Joe Chicago
3 Steve Madison
发生业务表
Tran_Id Con_ID Prod_ID Price
-------------------------------------
1 2 1 900
2 1 2 300
3 1 1 900
所需表
City Total Sales
----------------------
New York 1200
Chicago 900
Madison 0
试试这个,现在这个已经测试了
var query = from consumer in Tb_Consumer.AsEnumerable()
select new
{
CityName = consumer.Field<string>("City"),
//Name of relationship i.e. relation1 is important here
Sales = consumer.GetChildRows("relation1").Sum(tx=> tx.Field<decimal>("Price"))
};
var datatable = new DataTable();
col = new DataColumn("City");
datatable.Columns.Add(col);
col = new DataColumn("TotalSales");
datatable.Columns.Add(col);
foreach (var item in query.ToList())
{
var newrow = datatable.NewRow();
newrow["City"] = item.CityName;
newrow["TotalSales"] = item.Sales;
datatable.Rows.Add(newrow);
}