Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.Net LINQ - left outer join between two datatables - limit to one row

I have an issue with LINQ in vb.net. Basically I want create LEFT JOIN between two datatables.

This is the info of the two datatables:

Dim vDT1 As New DataTable
vDT1.Columns.Add("Key")
vDT1.Columns.Add("Data1")
vDT1.Columns.Add("Data2")

vDT1.Rows.Add({"01", "DATA1_AAAA", "DATA2_AAAA"})
vDT1.Rows.Add({"02", "DATA1_BBBB", "DATA2_BBBB"})

Dim vDT2 As New DataTable
vDT2.Columns.Add("Key")
vDT2.Columns.Add("Data3")
vDT2.Columns.Add("Data4")

vDT2.Rows.Add({"01", "DATA3_AAAA", "DATA4_AAAA"})
vDT2.Rows.Add({"01", "DATA3_BBBB", "DATA4_BBBB"})
vDT2.Rows.Add({"01", "DATA3_CCCC", "DATA4_CCCC"})
vDT2.Rows.Add({"01", "DATA3_DDDD", "DATA4_DDDD"})

Dim vDRnull As DataRow = vDTsec.Rows.Add

In order to achieve the LINQ JOIN I try to use the next sentence:

From a In vDT1.AsEnumerable
Group Join bTemp In vDT2.AsEnumerable
On a.Field(Of String)("Key") Equals bTemp.Field(Of String)("Key")
Into Group From b In Group.DefaultIfEmpty(vDRnull)
Select a, b

This is the result (but I want remove the red rows), I only want get the first occurrence in the second table:

Issue 1

Any help will be appreciated!


I edit the question with the correct method. Thanks to Jeff Mercado:

Dim vDT1 As New DataTable
vDT1.Columns.Add("Key")
vDT1.Columns.Add("Data1")
vDT1.Columns.Add("Data2")

vDT1.Rows.Add({"01", "DATA1_AAAA", "DATA2_AAAA"})
vDT1.Rows.Add({"02", "DATA1_BBBB", "DATA2_BBBB"})

Dim vDT2 As New DataTable
vDT2.Columns.Add("Key")
vDT2.Columns.Add("Data3")
vDT2.Columns.Add("Data4")

vDT2.Rows.Add({"01", "DATA3_AAAA", "DATA4_AAAA"})
vDT2.Rows.Add({"01", "DATA3_BBBB", "DATA4_BBBB"})
vDT2.Rows.Add({"01", "DATA3_CCCC", "DATA4_CCCC"})
vDT2.Rows.Add({"01", "DATA3_DDDD", "DATA4_DDDD"})

Dim vDRnull As DataRow = vDT2.Rows.Add

Dim vLINQ = From a In vDT1.AsEnumerable
    Group Join bTemp In vDT2.AsEnumerable
    On a.Field(Of Object)("Key") Equals bTemp.Field(Of Object)("Key")
    Into Group Let b = If(Group.FirstOrDefault Is Nothing, vDRnull, Group.FirstOrDefault)
    Select a, b

enter image description here

like image 690
MiBol Avatar asked Feb 19 '23 11:02

MiBol


1 Answers

The key is to call FirstOrDefault() on the group. Then you either have the first row in the group or Nothing.

Dim query =
    From a In vDT1.AsEnumerable
    Group Join b In vDT2.AsEnumerable
        On a.Field(Of String)("Key") Equals b.Field(Of String)("Key")
        Into Group
    Let b = Group.FirstOrDefault
    Select Key = a.Field(Of String)("Key"),
           Data1 = a.Field(Of String)("Data1"),
           Data2 = a.Field(Of String)("Data2"),
           Data3 = b?.Field(Of String)("Data3"),
           Data4 = b?.Field(Of String)("Data4")

    ' Or Simply
    'Select a, b
like image 104
Jeff Mercado Avatar answered Mar 09 '23 00:03

Jeff Mercado