是否有一种简单的方法可以在C#中进行INNER连接,OUTER连接,LEFT OUTER连接,RIGHT OUTER连接或UNION两个(或更多)DataTables?

时间:2016-07-27 20:14:24

标签: c# .net join datatable

我正在编写一个连接不同数据库系统的c#应用程序。这些系统可以是平面文件数据库,Oracle,Sql,Excel文件,分机。 C#应用程序的工作是提供一个插座,使所有这些源在一个位置可用。基本上,应用程序接受相应数据库系统的查询和连接设置列表,并收集大量结果。

目标是输出一个单一的DataTable,所有这些查询的结果都加入/联合在一起(取决于设置)。 C#是否提供了一种在DataTable列表上执行任何连接/联合操作的简便方法?

例如:

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  |
-----------------------------------------------------------------------------------------------------------   

到目前为止,我已在网上找到以下代码(here)对所有数据进行合并:

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中是否存在一种更简单的方法可以让我做 ANY 在一组单独的DataTables上加入或联合(不仅仅是上面代码中的联合),还是我必须自定义每种类型的join / union?

1 个答案:

答案 0 :(得分:2)

不,没有一种简单的.Net方式可以做到这一点......

LINQ可以接近......您可以在LINQ中创建表连接,但它们通常是&#34;内连接&#34;。做一个&#34;左连接&#34;有点复杂,需要GroupJoin关键字。 https://msdn.microsoft.com/en-us/library/bb386969(v=vs.110).aspx

如果你喜欢&#34;自己动手做&#34;使用ADO.Net DataRelations,您可以查看这篇旧的VB.Net文章:

http://www.emmet-gray.com/Articles/DataRelations.html

相关问题