【问题标题】:Linq Full outer join [duplicate]Linq完全外连接[重复]
【发布时间】:2017-06-10 15:16:42
【问题描述】:

大家好,我正在尝试从 2 个表中获取完整的外部联接

前:

Table_A 
--------
_a
_c

Table_B
------
_a
_b

结果应该是

Table_C
-------
_a
_b
_c

但我似乎没有得到实际结果

这就是我所做的

IEnumerable<string> leftOuter1 = (from watcher in _watchers
                                     join attendee in attendees on watcher equals attendee into set
                                     from attendee in set.DefaultIfEmpty()
                                     select attendee);

    IEnumerable<string> rightOuter1 = (from attendee in attendees
                                       join watcher in _watchers on attendee equals watcher into set
                                      from watcher in set.DefaultIfEmpty()
                                      select watcher);


    IEnumerable<string> participants1 = leftOuter.Union(rightOuter);

在 _watchers 中,我的值为“_a”。

在与会者中,值为“_a”和“_b”。

第一个结果是_a,但第二个结果是_a,null。我在这里做错了什么?

谢谢

【问题讨论】:

    标签: c# entity-framework linq linq-to-sql


    【解决方案1】:

    你可以看到这个

    var firstNames = new[]    {
    new { ID = 1, Name = "John" },
    new { ID = 2, Name = "Sue" },    };
    
    var lastNames = new[]
    {
    new { ID = 1, Name = "Doe" },
    new { ID = 3, Name = "Smith" },
    };
    var leftOuterJoin = from first in firstNames
                    join last in lastNames
                    on first.ID equals last.ID
                    into temp
                    from last in temp.DefaultIfEmpty(new { first.ID, Name =       default(string) })
                    select new
                    {
                        first.ID,
                        FirstName = first.Name,
                        LastName = last.Name,
                    };
    var rightOuterJoin = from last in lastNames
                     join first in firstNames
                     on last.ID equals first.ID
                     into temp
                     from first in temp.DefaultIfEmpty(new { last.ID, Name =     default(string) })
                     select new
                     {
                         last.ID,
                         FirstName = first.Name,
                         LastName = last.Name,
                     }; var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);
    

    LINQ - Full Outer Join

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 2018-05-30
      • 2018-07-12
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2015-03-31
      相关资源
      最近更新 更多