csharp-linq

Linq

FullOuterJoin

method 방식으로는 한번에 가능

1
static void Main(string[] args)
2
{
3
    var ax = new[] { 
4
        new { id = 1, name = "John" },
5
        new { id = 2, name = "Sue" } };
6
    var bx = new[] { 
7
        new { id = 1, surname = "Doe" },
8
        new { id = 3, surname = "Smith" } };
9
10
    ax.FullOuterJoin(bx, a => a.id, b => b.id, (a, b, id) => new {a, b})
11
        .ToList().ForEach(Console.WriteLine);
12
}

query 방식으로는 union method로 가능

1
var firstNames = new[]
2
{
3
    new { ID = 1, Name = "John" },
4
    new { ID = 2, Name = "Sue" },
5
};
6
var lastNames = new[]
7
{
8
    new { ID = 1, Name = "Doe" },
9
    new { ID = 3, Name = "Smith" },
10
};
11
var leftOuterJoin = from first in firstNames
12
                    join last in lastNames
13
                    on first.ID equals last.ID
14
                    into temp
15
                    from last in temp.DefaultIfEmpty(new { first.ID, Name = default(string) })
16
                    select new
17
                    {
18
                        first.ID,
19
                        FirstName = first.Name,
20
                        LastName = last.Name,
21
                    };
22
var rightOuterJoin = from last in lastNames
23
                     join first in firstNames
24
                     on last.ID equals first.ID
25
                     into temp
26
                     from first in temp.DefaultIfEmpty(new { last.ID, Name = default(string) })
27
                     select new
28
                     {
29
                         last.ID,
30
                         FirstName = first.Name,
31
                         LastName = last.Name,
32
                     };
33
var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);
Share