如何使用Inner Join然后用连接结果填充DataSet?

时间:2016-03-16 05:56:00

标签: c# database join

嗯,这是我的问题。简而言之;我有两个表,Consequents和Atomic propositions:

AtomicP表

 ID        Proposition  
    1     | A              | 
    1     | B              | 
    1     | C              |   
    2     | D              | 
    2     | E              |  

结果表

ID    |  Consequent    | 
1     | A              | 
2     | B              | 

我只想做,就是实现一个内连接,它给出了两个表的ID相同的所有值(即):

AtomicP Table  "A"   "B"   "C" -> "A" Consequent Table        

并将结果赋予内部联接的坦克,将结果保存在数据集或其他可能更好的数据结构中。

最好的问候。

4 个答案:

答案 0 :(得分:0)

假设目的地表具有值Id,Proposition和Consequent ..

insert into newtable (id,proposition,consequent) select id,atomicP,Consequent from atmicp,consequent where atomicP.id = consequent.id

答案 1 :(得分:0)

public class Proposition
{
    public int Id;
    public string Value;

    public Proposition(int id, string value){
       Id = id;
       Value = value;
    }
}

public class Consequent
{
   public int Id;
   public string Value;

   public Consequent(int id, string value){
     Id = id;
     Value = value;
   }
}

var atomicP = new List<Proposition>{
   new Proposition(1, "A"),
   new Proposition(1, "B"),
   new Proposition(1, "C"),
   new Proposition(2, "D"),
   new Proposition(2, "E"),
}

var consequents = new List<Consequent>{
   new Consequent(1, "A"),
   new Consequent(2, "B"),
}

var query = from proposition in atomicP
            join consequent in consequents on proposition.Id == consequent.Id
            select proposition.Value;

return query.ToList();          

答案 2 :(得分:0)

使用此功能

private DataTable JoinDataTables(DataTable t1, DataTable t2, params Func<DataRow, DataRow, bool>[] joinOn)
{
    DataTable result = new DataTable();
    foreach (DataColumn col in t1.Columns)
    {
        if (result.Columns[col.ColumnName] == null)
            result.Columns.Add(col.ColumnName, col.DataType);
    }
    foreach (DataColumn col in t2.Columns)
    {
        if (result.Columns[col.ColumnName] == null)
            result.Columns.Add(col.ColumnName, col.DataType);
    }
    foreach (DataRow row1 in t1.Rows)
    {
        var joinRows = t2.AsEnumerable().Where(row2 =>
            {
                foreach (var parameter in joinOn)
                {
                    if (!parameter(row1, row2)) return false;
                }
                return true;
            });
        foreach (DataRow fromRow in joinRows)
        {
            DataRow insertRow = result.NewRow();
            foreach (DataColumn col1 in t1.Columns)
            {
                insertRow[col1.ColumnName] = row1[col1.ColumnName];
            }
            foreach (DataColumn col2 in t2.Columns)
            {
                insertRow[col2.ColumnName] = fromRow[col2.ColumnName];
            }
            result.Rows.Add(insertRow);
        }
    }
    return result;
}

如何使用此示例:

var test = JoinDataTables(Consequents, Atomic,
               (row1, row2) =>
               row1.Field<int>("ID") == row2.Field<int>("ID"));

答案 3 :(得分:0)

我假设你想加入C#并得到DataTable(有点不清楚)。

代码段使用DataTable加入两个Linq并插入另一个表。

DataTable results = new DataTable();
results.Columns.Add("ID", typeof(int));
results.Columns.Add("Proposition", typeof(string));
results.Columns.Add("Consequent", typeof(string));

var result1 = from arow in AtomicP.AsEnumerable()
    join con in Consequent.AsEnumerable()
    on arow.Field<int>("ID") equals con.Field<int>("ID")
    select results.LoadDataRow(new object[]
                               {
                                   arow.Field<int>("ID"),
                                   arow.Field<string>("Proposition"),
                                   con.Field<string>("Consequent")
                               }, false);

现在我们可以通过迭代结果来访问结果。

foreach(DataRow row in results.Rows)
{
    foreach(DataColumn column in results.Columns)
    {
        //Console.WriteLine(row[column]);
    }
}

工作Code