【发布时间】:2012-12-11 05:00:05
【问题描述】:
我有 2 个数据表,它们具有相同的列但具有不同的数据,并且有时有些行共享相同的 ID
我想将那些表加入到表 2 中某些行的 ID 在表 1 中不存在的表中
so if i the following tables
ID Name
1 A
2 B
3 C
ID Name
5 D
1 A
2 B
3 C
the from joining would be
ID Name
1 A
2 B
3 C
5 D
这是我尝试过的
Dim q = From e In tbl1.AsEnumerable Join r In tbl2.AsEnumerable On e.Field(Of Integer)("id") Equals r.Field(Of Integer)("id")
但不知道如何将其保存到数据表中
【问题讨论】:
-
@MitchWheat 用我的代码更新问题
-
似乎 UNION 可能是更好的选择。 msdn.microsoft.com/en-us/library/bb341731.aspx
-
@MitchWheat 但我仍然使用
AsEnumerable并使用 union 或 join,如何将数据转换回数据表?! -
这取决于您的数据源提供者。
-
您是否需要以
DataTable的形式返回数据?有时IEnumerable<T>会给你更好的性能和减少内存占用。您可以使用 LINQ 轻松地从表中提取数据,并对 IEnumerable 对象执行UNION。
标签: vb.net linq join distinct union