Search code examples
c#.netlinq

C# Linq join between two sub tables


I have two tables

Existing table

{{ID: 1, Codes: { 1, 2, 3 }},
 {ID: 2, Codes: { 4, 5, 6, 7 }}, 
 {ID: 3, Codes: { 8, 9, 10, 11 }}}

and an incoming table

{{Codes: { 1, 2, 3 }},
 {Codes: { 4, 5, 6, 7 }},
 {Codes: { 13, 14, 15, 16 }}}

I want to get the new row out of the incoming data. So, new data can be inserted into my existing table. I want the putout to be the following:

 {{Codes: { 13, 14, 15, 16 }}}

Can someone help me write a Linq query?


Solution

  • Here you go:

    var newRows = incomingTable
        .Where(incomingRow => !existingTable
            .Any(existingRow => existingRow.Codes.Intersect(incomingRow.Codes).Any()));