即使外键为NULL,也要加入两个DataTable

时间:2014-07-23 17:36:04

标签: c# linq

我的应用程序中有2个DataTables。第一个DataTable称为Table1,看起来像这样

-------------------------------------
| Key | Column1 | Column2 | Foreign |
|-----------------------------------|
|  0  | dsfsfsd | sdfsrer |    1    |
|-----------------------------------|
|  1  | dertert | qweqweq |   NULL  |
|-----------------------------------|
|  2  | prwersd | xzcsdfw |    3    |
-------------------------------------

第二个被称为Table2,看起来像这样

----------------------------------------
|  Key   | Column3 | Column4 | Column5 |
|--------------------------------------|
|   1    | dsfsfsd | sdfsrer | fghfghg |
|--------------------------------------|
|   3    | prwersd | xzcsdfw | nbmkuyy |
----------------------------------------

所以我想使用LINQ在这两个表上进行内连接,以便连接表看起来像这样。如果链接到Table1的外键为NULL,我不想丢失Table2中的数据

---------------------------------------------------------
| Key | Column1 | Column2 | Column3 | Column4 | Column5 |
|-------------------------------------------------------|
|  0  | dsfsfsd | sdfsrer | dsfsfsd | sdfsrer | fghfghg |
|-------------------------------------------------------|
|  1  | dertert | qweqweq |  NULL   |  NULL   |  NULL   | // This row is missing in my application
|-------------------------------------------------------|
|  2  | prwersd | xzcsdfw | prwersd | xzcsdfw | nbmkuyy |
---------------------------------------------------------

这是我试过的

var query = from table1Row in Table1.AsEnumerable()
            join table2Row in Table2.AsEnumerable()
                on table1Row.Foreign equals table2Row.Key
            select new SelectedColumns
            {
                Column1 = table1Row.Column1
                Column2 = table1Row.Column2
                Column3 = table2Row.Column3
                Column4 = table2Row.Column4
                Column5 = table2Row.Column5
            }

但是,此LINQ查询会跳过没有匹配项的记录。我该怎么做才能得到上面的结果?


解决方案:因此C.J.的回答指出了我正确的方向。我将查询更改为

var query = from table1Row in Table1.AsEnumerable()
            join table2Row in Table2.AsEnumerable()
                on table1Row.Foreign equals table2Row.Key into leftJoin
            from table2Row in leftJoin.DefaultIfEmpty()
            select new SelectedColumns
            {
                Column1 = table1Row.Column1
                Column2 = table1Row.Column2
                Column3 = table2Row.Column3
                Column4 = table2Row.Column4
                Column5 = table2Row.Column5
            }

然而,现在它抛出异常Value cannot be null. Parameter name: row。事实证明,当您选择所需的字段时,需要使用布尔表达式来检查该值是否为NULL。所以我将该部分更新为

Column1 = table1Row.Field<COLUMN_TYPE?>("COLUMN_TYPE")
Column2 = table1Row.Field<COLUMN_TYPE?>("Column2")
Column3 = table2Row == null ? (COLUMN_TYPE?)null : table2Row.Field<COLUMN_TYPE?>("Column3")
Column4 = table2Row == null ? (COLUMN_TYPE?)null : table2Row.Field<COLUMN_TYPE?>("Column4")
Column5 = table2Row == null ? (COLUMN_TYPE?)null : table2Row.Field<COLUMN_TYPE?>("Column5")

它有效。确保将COLUMN_TYPE更新为该特定列的类型。 SelectedColumns中的对象也都是Nullable<>

1 个答案:

答案 0 :(得分:3)

您需要使用Left Outer Join

中的LINQ

请参阅此页:http://msdn.microsoft.com/en-US/vstudio/ee908647.aspx#leftouterjoin