使用LINQ使用两个键列来连接两个数据表

时间:2016-04-14 15:17:42

标签: c# .net linq join datatable

数据表A:

Key1 | Key2 | A |乙

[1 1 ...]

[1 2 ...]

数据表B:

Key1 | Key2 | x | ÿ

[1 1 ...]

[1 2 ...]

期望的结果:

Key1 | Key2 | A | B | X | ÿ

[1 1 ...]

[1 2 ...]

在最终结果中,列A,B,X和Y已添加到新数据表中。之所以发生这种情况,是因为key1和key2在数据表A和B中都相等。在给定条件的情况下,这可能与完全外连接有关 - (Key1和Key2是euqal)?

2 个答案:

答案 0 :(得分:1)

试试这位代码朋友:

override   func drawRect(rect: CGRect) {
  UIColor.greenColor().setFill();
}

答案 1 :(得分:1)

NoReverseMatch: Reverse for 'upload' with arguments '()' and keyword arguments '{u'token': u'1234'}' not found. 0 pattern(s) tried: []

假设Key1和Key2为var list1 = (from t1 in dataTable1.AsEnumerable() select new { Key1 = t1.Field<int>("Key1"), Key2 = t1.Field<int>("Key2"), A = t1.Field<string>("A"), B = t1.Field<string>("B") }); var list2 = (from b in dataTable2.AsEnumerable() select new { Key1 = b.Field<int>("Key1"), Key2 = b.Field<int>("Key2"), X = b.Field<string>("X"), Y = b.Field<string>("Y") }); // Now join the 2 collections and get the result you want. var result = (from x in list1 join y in list2 on new { x.Key1,x.Key2} equals new { y.Key1,y.Key2 } select new { A = x.A, X = y.X }).ToList(); 类型且A.B,X和Y为int类型。