简体   繁体   English

与多个数据表的左外连接

[英]Left Outer Join with multiple Data tables

I have 3 DataTables我有 3 个数据表

DataTable1数据表1

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数据表2

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数据表3

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

Expected Join预期加入

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

I am doing a Join Operation on these tables using Linq as follows:我正在使用 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
    }

I am getting an exception of datarowc to be null.我得到一个datarowc的例外是null。 Not quite sure why datarowc is null here and how to achieve the expected join.不太清楚为什么 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() performs inner join, but you want left outer join, so forget about.Join() Code I've provided gives you the result you expect. .Join() 执行内连接,但你想要左外连接,所以忘记我提供的 .Join() 代码会给你你期望的结果。 But maybe you need to add one more Select to form datastructure you need.但也许您需要再添加一个 Select 来形成您需要的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM