1

我在两个表上进行交叉连接时遇到问题。这是我的代码:

public DataTable JoinTable(DataTable piTable1, DataTable piTable2)
{
    if (piTable1 == null || piTable2 == null)
        return new DataTable();

    var joinResult = from parent in piTable1.Rows.Cast<DataRow>()
                     from child in piTable2.Rows.Cast<DataRow>()
                     select new {parent, child};

    return joinResult.CopyToDataTable();
}

我得到的结果是两个DataRowCollections,但它无法将其转换为 a DataTable。如何在两个DataTables 上进行交叉连接?

4

3 回答 3

0

试试这个方法

public DataTable JoinTable(DataTable piTable1, DataTable piTable2)
{
    DataTable JoinTable = new DataTable();

    if (piTable1 == null || piTable2 == null)
        return new DataTable();

    var q = from parent in piTable1.AsEnumerable()
                     from child in piTable2.AsEnumerable()
                     select new 
                     {                                                      
                         property1 = parent.Field<T>("PropertyName1"),
                         property2 = parent.Field<T>("PropertyName2"),
                         property3 = child.Field<T>("PropertyName3"),
                         property4 = child.Field<T>("PropertyName4")
                     };

    JoinTable.Rows.Add(q);
    return JoinTable;
}
于 2012-11-29T14:08:45.363 回答
0

您只需要手动完成:
1)创建结果 DataTable
2)从两个表中添加列(请记住,输入表中的列名可能具有保存名称,因此您需要约定来处理它 - 比如包括输入表名或smth)
3) 并将值从连接结果实体复制到相应的列

于 2012-11-29T07:39:28.223 回答
0

我使用了下面对我有用的代码。我不得不进一步将 q 转换为数组,然后列出,然后是表格,因为我无法将 q 直接转换为表格。以下是可能对某人有所帮助的代码。

public DataTable JoinTable(DataTable piTable1, string table1Column, 
DataTable piTable2, string table2Column)
{
     DataTable JoinTable = new DataTable();
     JoinTable.Columns.Add(table1Column);
     JoinTable.Columns.Add(table2Column);

        if (piTable1 == null || piTable2 == null)
            return new DataTable();

        var q = from parent in piTable1.AsEnumerable()
                from child in piTable2.AsEnumerable()
                select new
                {
                    table1Column = parent.Field<string>(table1Column),
                    table1Column = child.Field<string>(table2Column),
                };

        JoinTable = ToDataTable((q.ToArray()).ToList());
        JoinTable.TableName = "ResultTable";

        return JoinTable;

    }

    public DataTable ToDataTable<T>(List<T> items)
    {
        DataTable dataTable = new DataTable(typeof(T).Name);
        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name);
        }
        foreach (T item in items)

        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        //put a breakpoint here and check datatable
        return dataTable;
    }
于 2017-11-23T13:16:33.560 回答