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

前端 未结 1 1387
天命终不由人
天命终不由人 2020-12-11 08:23

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 Ne         


        
相关标签:
1条回答
  • 2020-12-11 08:53

    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 = If(b Is Nothing, Nothing, b.Field(Of String)("Data3")),
               Data4 = If(b Is Nothing, Nothing, b.Field(Of String)("Data4"))
    
        ' Or Simply
        'Select a, b
    
    0 讨论(0)
提交回复
热议问题