有没有一种简单的方法可以在C#中实现INNER联接、OUTER联接、LEFT OUTER联接,RIGHT OUTER联接
本文关键字:联接 OUTER 实现 INNER RIGHT LEFT 一种 简单 方法 有没有 | 更新日期: 2023-09-27 17:59:02
我正在编写一个连接独立数据库系统的c#应用程序。这些系统可以是平面文件数据库、Oracle、Sql、Excel文件等。C#应用程序的工作是提供一个出口,使所有这些源在一个位置可用。因此,基本上,应用程序接受相应数据库系统的查询和连接设置列表,并收集一组结果。
目标是输出一个单独的DataTable,其中包含所有这些查询的结果(取决于设置)。C#是否提供了一种在数据表列表上执行任何联接/并集操作的简单方法?
例如:
Table1:
__________________________________________________________
|tb1_pk_id| tb1_name | tb1_data1 | tb1_data2 |
|---------|---------------|---------------|---------------|
| 1 | tb1name_blah1 | tb1dat1_blah1 | tb1dat2blah1 |
| 2 | tb1name_blah2 | tb1dat1_blah2 | tb1dat2blah2 |
| 3 | tb1name_blah3 | tb1dat1_blah3 | tb1dat2blah3 |
-----------------------------------------------------------
Table2:
__________________________________________________________
|tb2_pk_id| tb2_name | tb2_data1 | tb2_data2 |
|---------|---------------|---------------|---------------|
| 1 | tb2name_blah1 | tb2dat1_blah1 | tb2dat2blah1 |
| 2 | tb2name_blah2 | tb2dat1_blah2 | tb2dat2blah2 |
| 3 | tb2name_blah3 | tb2dat1_blah3 | tb2dat2blah3 |
-----------------------------------------------------------
Join Results:
__________________________________________________________ _______________________________________________
|tb1_pk_id| tb1_name | tb1_data1 | tb1_data2 | tb2_name | tb2_data1 | tb2_data2 |
|---------|---------------|---------------|---------------|---------------|---------------|---------------|
| 1 | tb1name_blah1 | tb1dat1_blah1 | tb1dat2blah1 | tb2name_blah1 | tb2dat1_blah1 | tb2dat2blah1 |
| 2 | tb1name_blah2 | tb1dat1_blah2 | tb1dat2blah2 | tb2name_blah2 | tb2dat1_blah2 | tb2dat2blah2 |
| 3 | tb1name_blah3 | tb1dat1_blah3 | tb1dat2blah3 | tb2name_blah3 | tb2dat1_blah3 | tb2dat2blah3 |
-----------------------------------------------------------------------------------------------------------
到目前为止,我在网上(这里)找到了以下代码来对所有数据进行合并:
private DataTable MergeAll(IList<DataTable> tables, String primaryKeyColumn)
{
if (!tables.Any())
throw new ArgumentException("Tables must not be empty", "tables");
if (primaryKeyColumn != null)
foreach (DataTable t in tables)
if (!t.Columns.Contains(primaryKeyColumn))
throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
if (tables.Count == 1)
return tables[0];
DataTable table = new DataTable("TblUnion");
table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
foreach (DataTable t in tables)
{
table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
}
table.EndLoadData();
if (primaryKeyColumn != null)
{
// since we might have no real primary keys defined, the rows now might have repeating fields
// so now we're going to "join" these rows ...
var pkGroups = table.AsEnumerable()
.GroupBy(r => r[primaryKeyColumn]);
var dupGroups = pkGroups.Where(g => g.Count() > 1);
foreach (var grpDup in dupGroups)
{
// use first row and modify it
DataRow firstRow = grpDup.First();
foreach (DataColumn c in table.Columns)
{
if (firstRow.IsNull(c))
{
DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
if (firstNotNullRow != null)
firstRow[c] = firstNotNullRow[c];
}
}
// remove all but first row
var rowsToRemove = grpDup.Skip(1);
foreach (DataRow rowToRemove in rowsToRemove)
table.Rows.Remove(rowToRemove);
}
}
return table;
}
这对于执行并集来说很好,但我不知道.NET中是否已经存在一种更简单的方法,可以让我在一组seprate DataTables(而不仅仅是上面代码中的并集)上执行ANY类型的联接或并集,或者我必须自定义每种类型的联接/并集的代码吗?
不,没有简单的.Net方法。。。。
LINQ可以接近。。。您可以在LINQ中创建表联接,但它们通常是"内部联接"。执行"左联接"有点复杂,并且需要GroupJoin
关键字。https://msdn.microsoft.com/en-us/library/bb386969(v=vs.110).aspx
如果你想用ADO.Net DataRelations"自己动手",你可以看看这篇VB.Net的老文章:
http://www.emmet-gray.com/Articles/DataRelations.html