1

我有 3 个数据表,其中填充了数据集和表适配器/绑定源,我需要运行 Join 查询或找到另一种获取特定数据的方法。(数据集包含列出的每个表)

表:

产品表:

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

给定一个产品名称,我需要用每个不同的城市以及该产品在该城市的售价填充一个表格(将该产品的所有价格添加到给定城市的任何消费者)

我真的很难过,找不到办法。(我试过很多)请帮忙,谢谢!

到目前为止,Nudiers 的方法是:

            DataRelation relation = null;
            DataColumn table1Column = null;
            DataColumn table2Column = null;
            DataColumn table3Column = null;

            table1Column = tlobergeDataSet.Tb_Product.Columns[0];
            table2Column = tlobergeDataSet.Tb_Transactions.Columns[3];
            table3Column = tlobergeDataSet.Tb_Consumer.Columns[0];

            relation = new DataRelation("relation", table1Column, table2Column);
            tlobergeDataSet.Relations.Add(relation);
4

4 回答 4

2
    public DataTable MergeTables(DataTable dtFirst, DataTable dtSecond, string CommonColumn)
    {
        DataTable dtResults = dtFirst.Clone();
        int count = 0;
        for (int i = 0; i < dtSecond.Columns.Count; i++)
        {
            if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
            {
                dtResults.Columns.Add(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
                count++;
            }
        }

        DataColumn[] columns = new DataColumn[count];
        int j = 0;
        for (int i = 0; i < dtSecond.Columns.Count; i++)
        {
            if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
            {
                columns[j++] = new DataColumn(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
            }
        }

        dtResults.BeginLoadData();
        foreach (DataRow dr in dtFirst.Rows)
        {
            dtResults.Rows.Add(dr.ItemArray);
        }
        foreach (DataRow dr in dtSecond.Rows)
        {
            foreach (DataRow dr1 in dtResults.Rows)
            {
                if (dr1[CommonColumn].ToString().Equals(dr[CommonColumn].ToString()))
                {
                    foreach (DataColumn c in columns)
                    {
                        dr1[c.ColumnName] = dr[c.ColumnName];
                    }
                }
            }
        }
        dtResults.EndLoadData();
        return dtResults;
    }
于 2012-06-25T10:30:27.383 回答
0

试试这个。

DataRelation relation = null;
DataColumn table1Column = null;
DataColumn table2Column = null;
//retrieve column
table1Column = ds.Tables("Table1").Columns(0);
table2Column = ds.Tables("table2").Columns(0);
//relating tables
relation = new DataRelation("relation", table1Column, table2Column);
//assign relation to dataset
ds.Relations.Add(relation);
于 2012-04-17T16:09:17.547 回答
0

您只能在 DataRelation 对象中关联两个表,并且从数据集中访问数据很简单,因为数据已经关联。

        DataRelation relation = null;
        DataColumn table1Column = null;
        DataColumn table2Column = 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);
于 2012-04-17T16:46:47.280 回答
0

在 LINQ 中,您可以使用如下语法连接表以查找所需的数据:

from a in keyTable
join b in anotherTable on a.Key equals b.Key
join c in aThirdTable on a.Key equals c.Key
select new
{
    // Anonymous Object Properties using identifier a, b, and c to get data
};

您应该能够获取该片段并生成一个 linq 查询,该查询将生成一个包含您需要的特定数据表示的匿名对象。

于 2012-04-17T16:00:18.357 回答