如何通过列名将两个DataRow连接到一个DataRow?

时间:2015-12-07 04:39:39

标签: c# asp.net

我试图弄清楚如何通过部门列名称将两个相应的数据行连接到数据集中的单个数据行。

在提供的数据集输出中,我想通过列名称将Gastroenterology和Medical Gastroen(两个datrows)连接到单个数据行(类似于合并行的必需最终数据集输出)。

需要你的想法/帮助在asp.net和/或C#中完成它。

数据集输出

     Department    Male Visit    Female Visit    Total Count
     ----------    ----------    ------------    -----------
    Endocrinology     10             20              30
  Gastroenterology    15             25              40
  General Medicine    25             05              30
  Medical Gastroen    30             20              50

合并行的最终数据集输出

     Department    Male Visit    Female Visit    Total Count
     ----------    ----------    ------------    -----------
    Endocrinology     10             20              30
  Gastroenterology    45             45              90
  General Medicine    25             05              30

2 个答案:

答案 0 :(得分:-1)

我认为你必须在数据库查询中使用加入。那会更好。 表A

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

表B.

+-----+---------------------+-------------+--------+
| OID | DATE                |          ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           2 |   1560 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
+-----+---------------------+-------------+--------+

SQL QUERY     SQL> SELECT ID,NAME,AMOUNT,DATE          来自客户          内部联系订单          ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

结果表:

+----+----------+--------+---------------------+
| ID | NAME     | AMOUNT | DATE                |
+----+----------+--------+---------------------+
|  3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|  3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|  2 | Khilan   |   1560 | 2009-11-20 00:00:00 |
|  4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+

我希望它会对你有所帮助。

答案 1 :(得分:-1)

你可以这样做。

        DataTable _dataTable = new DataTable();
        DataRow _dataRow1 = null;

        _dataTable.TableName = "Products";
        _dataTable.Columns.Add("ID",typeof(int));
        _dataTable.Columns[0].AutoIncrementSeed = 1;
        _dataTable.Columns[0].AutoIncrement = true;
        _dataTable.Columns.Add("ProductsName");
        _dataTable.Columns.Add("Price");

        _dataRow1 = _dataTable.NewRow();
        _dataRow1["ProductsName"] = "Sony Laptop";
        _dataRow1["Price"] = "15000";

        _dataTable.Rows.Add(_dataRow1);

        DataRow _dataRow2 = null;
        _dataRow2 = _dataTable.NewRow();
        _dataTable.Rows.Add(_dataRow2);
        _dataRow2["ProductsName"] = "LG Laptop";
        _dataRow2["Price"] = "15000";

        DataSet _dataSet = new DataSet();
        _dataSet.Tables.Add(_dataTable);