左外连接与多个数据表

Left Outer Join with multiple Data tables

我有 3 个数据表

DataTable1

Id  Version     URL     Owner
1   1           "xx"    "alice" 
2   1           "yy"    "bob"
3   1           "zz"    "Mike"
4   1           "ww"    "Rob"
5   1           "ww"    "Bick"

DataTable2

Id  Version     DomainID    Region      Type
1   1           aa          asia        1
2   1           bb          europe      2
3   1           cc          africa      1
4   1           dd          aus1        0

DataTable3

Id  Size    FreeSpace
aa  2500    2000
bb  3300    3000
cc  5500    50

预计加入

Id  Version     URL     Owner       DomainID    Region      Type    Size    Freespace
1   1           "xx"    "alice"     aa          asia        1       2500    2000
2   1           "yy"    "bob"       bb          europe      2       3300    3000
3   1           "zz"    "Mike"      cc          africa      1       5500    50
4   1           "ww"    "sean"      dd          aus1        0       null    null    
5   1           "ww"    "Bick"      null        null        null    null    null

我正在使用 Linq 对这些表进行连接操作,如下所示:

// Datatable1 joins with Datatable2 on Id and version (datatable1)  -->  Id and version (datatable2) 
   // Datatable2 joins with Datatable3 on DomainId(datatable2) --> Id(datatable3)


var result = from dataRows1 in DataTable1.AsEnumerable()
                             join dataRows2 in DataTable2.AsEnumerable() on

                             new
                             {
                                 Id = dataRows1.Field<long>("Id"),
                                 Version = dataRows1.Field<long>("version")
                             } equals new
                             {
                                 Id = dataRows2.Field<long>("Id"),
                                 Version = dataRows2.Field<long>("version")
                             }
                              into tempJoin
                              from datarowc in tempJoin.DefaultIfEmpty() 
                             join dataRows3 in DataTable3.AsEnumerable() on                  
         dataRowsc.Field<long>("DomainId") equals dataRows3.Field<long>("Id")
    select new

    {
    datarow1,
    datarowc,
    datarow3
    }

我收到 datarowc 为空的异常。 不太清楚为什么 datarowc 在这里为 null 以及如何实现预期的连接。

using System.Data;
using System.Linq;

namespace CodeWars
{
    class Program
    {
        static void Main(string[] args)
        {            
            var result = datarows1.AsEnumerable()
                .Select(x => new
                    {
                        Tab1Row = x,
                        Tab2Row = datarows2.AsEnumerable().FirstOrDefault(
                            y => x.Field<int>("Id") == y.Field<int>("Id") &&
                                x.Field<int>("Version") == y.Field<int>("Version")
                        )
                    }
                )
                .Select(x => new
                    {
                        Tab1Row = x.Tab1Row,
                        Tab2Row = x.Tab2Row,
                        Tab3Row = datarows3.AsEnumerable().FirstOrDefault(
                            y => x?.Tab2Row?.Field<string>("DomainId") == y.Field<string>("Id")
                        )
                    }
                );
        }

        static DataTable datarows1 = new DataTable
        {
            Columns = {
                    { "Id", typeof(int) },
                    { "Version", typeof(int) },
                    { "URL", typeof(string) },
                    { "Owner", typeof(string) },
                },
            Rows = {
                    { 1, 1, "xx", "alice" },
                    { 2, 1, "yy", "bob" },
                    { 3, 1, "vv", "mike" },
                    { 4, 1, "ww", "rob" },
                    { 5, 1, "zz", "bick" },
                }
        };

        static DataTable datarows2 = new DataTable
        {
            Columns = {
                    { "Id", typeof(int) },
                    { "Version", typeof(int) },
                    { "DomainID", typeof(string) },
                    { "Region", typeof(string) },
                    { "Type", typeof(int) },
                },
            Rows = {
                    { 1, 1, "aa", "asia", 1 },
                    { 2, 1, "bb", "europe", 2},
                    { 3, 1, "cc", "asia", 1},
                    { 4, 1, "dd", "aus1", 0},
                }
        };

        static DataTable datarows3 = new DataTable
        {
            Columns = {
                    { "Id", typeof(string) },
                    { "Size", typeof(int) },
                    { "FreeSpace", typeof(int) },
                },
            Rows = {
                    { "aa", 2500, 2000 },
                    { "bb", 3300, 3000 },
                    { "cc",5500, 50},
                }
        };

    }
}

.Join() 执行内连接,但你想要左外连接,所以忘记 .Join() 我提供的代码可以为您提供预期的结果。但也许你需要再添加一个 Select 来形成你需要的数据结构。