DOBON.NETプログラミング道掲示板

No34393 の記事


■34393 / )  こんな演算子初めて見ました。
□投稿者/ VBはじめました 一般人(22回)-(2019/11/25(Mon) 16:06:55)
  • アイコン環境/言語:[VisualStudio2017 VB.net] 
    分類:[.NET] 

    左外部結合の実行に載っていた次のコードに対しての質問です。
    https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins
    
    (質問1)
    コメント@のKeyは、必要でしょうか?削除しても、同じ動作をしますが、動作を明示するためにわざわざつけた方がよいという意味でしょうか?
    
    (質問2)
    コメントAにある二項演算子で使われているものですが、VBに、こんな演算子ってありましたけ?subpet?.Name
    ビルドすると問題なく動作します。
    
    注:このコードは、C#→VBの変換をWebで行ったものを整理したものです。
    
    詳しい方いらっしゃいましたら、教えて頂けないでしょうか?
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim magnus As Person =
                New Person With {.FirstName = "Magnus", .LastName = "Hedlund"}
            Dim terry As Person =
                New Person With {.FirstName = "Terry", .LastName = "Adams"}
            Dim charlotte As Person =
                New Person With {.FirstName = "Charlotte", .LastName = "Weiss"}
            Dim arlene As Person =
                New Person With {.FirstName = "Arlene", .LastName = "Huff"}
    
            Dim barley As Pet =
                New Pet With {.Name = "Barley", .Owner =
                    terry}
            Dim boots As Pet =
                New Pet With {.Name = "Boots", .Owner =
                    terry}
            Dim whiskers As Pet =
                New Pet With {.Name = "Whiskers", .Owner =
                    charlotte}
            Dim bluemoon As Pet =
                New Pet With {.Name = "Blue Moon", .Owner =
                    terry}
            Dim daisy As Pet =
                New Pet With {.Name = "Daisy", .Owner =
                    magnus}
    
            Dim people2 As List(Of Person) =
                New List(Of Person) From {
                    magnus,
                    terry,
                    charlotte,
                    arlene}
            Dim pets2 As List(Of Pet) =
                New List(Of Pet) From {
                    barley,
                    boots,
                    whiskers,
                    bluemoon,
                    daisy}
    
            Dim query = From person In people2
                        Group Join pet In pets2
                            On person Equals pet.Owner
                            Into gj = Group
                        From subpet In gj.DefaultIfEmpty()
                        Select New With {
                            person.FirstName,
                           Key .PetName = '@
                            If(subpet?.Name, String.Empty)} 'A
    
            For Each v In query
                Console.WriteLine($"{v.FirstName & ":"}{v.PetName}")
            Next
            'OutPut:
            'Magnus: daisy
            'Terry:  barley
            'Terry:  boots
            'Terry:  Blue Moon
            'Charlotte: whiskers
            'Arlene:
        End Sub
    
        Class Person
            Public Property FirstName As String
            Public Property LastName As String
        End Class
    
        Class Pet
            Public Property Name As String
            Public Property Owner As Person
        End Class
    
    End Class
    

違反を報告
返信 削除キー/


Mode/  Pass/


- Child Tree -