Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Ha a LINQ használatával nem általános IEnumerable gyűjteményeket kérdez le, például ArrayList, explicit módon deklarálnia kell a tartományváltozó típusát, hogy az tükrözze a gyűjtemény objektumtípusát. Ha például egy ArrayListStudent objektummal rendelkezik, a From záradéknak így kell kinéznie:
Dim query = From student As Student In arrList
'...
A tartományváltozó típusának megadásával az ArrayList egyes elemeit Student típusúvá konvertálja.
A lekérdezési kifejezésben explicit módon beírt tartományváltozó használata egyenértékű a metódus meghívásával Cast . Cast kivételt jelez, ha a megadott leadás nem hajtható végre. Cast és OfType a két standard lekérdezési operátor metódus, amelyek nem általános IEnumerable típusokon működnek. A Visual Basicben explicit módon meg kell hívnia az Cast adatforrás metódusát egy adott tartományváltozótípus biztosításához. További információ: Típuskapcsolatok a lekérdezési műveletekben (Visual Basic).
példa
Az alábbi példa bemutat egy egyszerű lekérdezést egy ArrayList felett. Vegye figyelembe, hogy ez a példa objektum inicializálókat használ, amikor a kód meghívja a Add metódust, de ez nem követelmény.
Imports System.Collections
Imports System.Linq
Module Module1
Public Class Student
Public Property FirstName As String
Public Property LastName As String
Public Property Scores As Integer()
End Class
Sub Main()
Dim student1 As New Student With {.FirstName = "Svetlana",
.LastName = "Omelchenko",
.Scores = New Integer() {98, 92, 81, 60}}
Dim student2 As New Student With {.FirstName = "Claire",
.LastName = "O'Donnell",
.Scores = New Integer() {75, 84, 91, 39}}
Dim student3 As New Student With {.FirstName = "Cesar",
.LastName = "Garcia",
.Scores = New Integer() {97, 89, 85, 82}}
Dim student4 As New Student With {.FirstName = "Sven",
.LastName = "Mortensen",
.Scores = New Integer() {88, 94, 65, 91}}
Dim arrList As New ArrayList()
arrList.Add(student1)
arrList.Add(student2)
arrList.Add(student3)
arrList.Add(student4)
' Use an explicit type for non-generic collections
Dim query = From student As Student In arrList
Where student.Scores(0) > 95
Select student
For Each student As Student In query
Console.WriteLine(student.LastName & ": " & student.Scores(0))
Next
' Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
End Module
' Output:
' Omelchenko: 98
' Garcia: 97