【问题标题】:Left Outer Join with multiple Data tables与多个数据表的左外连接
【发布时间】:2020-05-24 22:29:49
【问题描述】:

我有 3 个数据表

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

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

数据表3

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 在这里为空以及如何实现预期的连接。

【问题讨论】:

    标签: c# linq datatables-1.10


    【解决方案1】:
    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 来形成你需要的数据结构。

    【讨论】:

    • 效果很好,非常感谢!! :-) 。您对使用 join vs select 的意见非常有帮助。
    • 很酷,我很有帮助 :)让世界变得更美好——如果你可以使用类,就永远不要使用表 :) IDE 在我用表编写 linq 条件时帮助了我 0 次(零!!!)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多