1

我需要一些帮助来从数据集中获取数据。代码:

            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
4

1 回答 1

2

试试这个,现在已经测试过了

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);
}   
于 2012-04-18T04:18:38.930 回答