Linq-quantifier-operations

提供:Dev Guides
移動先:案内検索

LINQの数量詞操作

これらの演算子はブール値、つまり シーケンス内の一部またはすべての要素が特定の条件を満たす場合に真または偽。

Operator Description C# Query Expression Syntax VB Query Expression Syntax
All Returns a value ‘True’ if all elements of a sequence satisfy a predicate condition Not Applicable Aggregate … In … Into All(…)
Any Determines by searching a sequence that whether any element of the same satisfy a specified condition Not Applicable Aggregate … In … Into Any()
Contains Returns a ‘True’ value if finds that a specific element is there in a sequence if the sequence doe not contains that specific element , ‘false’ value is returned Not Applicable Not Applicable

すべての例-All(Of TSource)拡張メソッド

VB

Module Module1

   Sub Main()

      Dim barley As New Pet With {.Name = "Barley", .Age = 4}
      Dim boots As New Pet With {.Name = "Boots", .Age = 1}
      Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
      Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
      Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

      Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
      Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
      Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

      Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

      Dim query = From pers In people
                  Where (Aggregate pt In pers.Pets Into All(pt.Age > 2))
                  Select pers.Name

      For Each e In query
         Console.WriteLine("Name = {0}", e)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()

   End Sub

   Class Person
      Public Property Name As String
      Public Property Pets As Pet()
   End Class

   Class Pet
      Public Property Name As String
      Public Property Age As Integer
   End Class

End Module

VBの上記のコードがコンパイルされて広告が実行されると、次の結果が生成されます-

Arlene
Rui

Press any key to continue.

Any-拡張メソッドの例

VB

Module Module1

   Sub Main()

      Dim barley As New Pet With {.Name = "Barley", .Age = 4}
      Dim boots As New Pet With {.Name = "Boots", .Age = 1}
      Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
      Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
      Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

      Dim charlotte As New Person With {.Name = "Charlotte", .Pets = New Pet() {barley, boots}}
      Dim arlene As New Person With {.Name = "Arlene", .Pets = New Pet() {whiskers}}
      Dim rui As New Person With {.Name = "Rui", .Pets = New Pet() {bluemoon, daisy}}

      Dim people As New System.Collections.Generic.List(Of Person)(New Person() {charlotte, arlene, rui})

      Dim query = From pers In people
                  Where (Aggregate pt In pers.Pets Into Any(pt.Age > 7))
                  Select pers.Name

      For Each e In query
         Console.WriteLine("Name = {0}", e)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()

   End Sub

   Class Person
      Public Property Name As String
      Public Property Pets As Pet()
   End Class

   Class Pet
      Public Property Name As String
      Public Property Age As Integer
   End Class

End Module

VBの上記のコードがコンパイルされて広告が実行されると、次の結果が生成されます-

Rui

Press any key to continue.