Ricerca…


introduzione

LINQ (Language Integrated Query) è un'espressione che recupera i dati da un'origine dati. LINQ semplifica questa situazione offrendo un modello coerente per lavorare con i dati attraverso vari tipi di fonti e formati di dati. In una query LINQ, si lavora sempre con gli oggetti. Si utilizzano gli stessi schemi di codifica di base per interrogare e trasformare i dati in documenti XML, database SQL, set di dati ADO.NET, raccolte .NET e qualsiasi altro formato per il quale sia disponibile un provider LINQ.

Proiezione

' sample data
Dim sample = {1, 2, 3, 4, 5}

' using "query syntax"
Dim squares = From number In sample Select number * number

' same thing using "method syntax"
Dim squares = sample.Select (Function (number) number * number)

Possiamo anche proiettare più risultati contemporaneamente

Dim numbersAndSquares =
    From number In sample Select number, square = number * number

Dim numbersAndSquares =
    sample.Select (Function (number) New With {Key number, Key .square = number * number})

Selezione dall'array con condizioni semplici

Dim sites() As String = {"Stack Overflow", "Super User", "Ask Ubuntu", "Hardware   Recommendations"}
Dim query = From x In sites Where x.StartsWith("S")
' result = "Stack Overflow", "Super User"

Query sarà un oggetto enumerabile contenente Stack Overflow e Super User . x nella query sta iterando la variabile dove verrà archiviato ogni oggetto controllato dalla clausola Where .

Mappatura dell'array in base alla clausola Select

Dim sites() As String = {"Stack Overflow", 
                         "Super User", 
                         "Ask Ubuntu", 
                         "Hardware Recommendations"}
Dim query = From x In sites Select x.Length
' result = 14, 10, 10, 24

Il risultato della query sarà un oggetto enumerabile contenente lunghezze di stringhe nell'array di input. In questo esempio, i valori 14, 10, 10, 24. x nella query sono variabili iterating in cui verrà archiviato ogni oggetto dall'array di input.

Ordinazione dell'output

Dim sites() As String = {"Stack Overflow", 
                         "Super User", 
                         "Ask Ubuntu", 
                         "Hardware Recommendations"}

Dim query = From x In sites 
            Order By x.Length

' result = "Super User", "Ask Ubuntu", "Stack Overflow", "Hardware Recommendations"

La clausola OrderBy ordina l'output in base al valore restituito dalla clausola. In questo esempio è Lunghezza di ogni stringa. L'ordine di output predefinito è crescente. Se è necessario scendere, è possibile specificare la parola chiave Descending dopo la clausola.

Dim query = From x In sites
            Order By x.Length Descending

Generazione del dizionario da IEnumerable

' Just setting up the example
Public Class A
    Public Property ID as integer
    Public Property Name as string
    Public Property OtherValue as Object
End Class

Public Sub Example()
    'Setup the list of items
    Dim originalList As New List(Of A)
    originalList.Add(New A() With {.ID = 1, .Name = "Item 1", .OtherValue = "Item 1 Value"})
    originalList.Add(New A() With {.ID = 2, .Name = "Item 2", .OtherValue = "Item 2 Value"})
    originalList.Add(New A() With {.ID = 3, .Name = "Item 3", .OtherValue = "Item 3 Value"})

    'Convert the list to a dictionary based on the ID
    Dim dict As Dictionary(Of Integer, A) = originalList.ToDictionary(function(c) c.ID, function(c) c)

    'Access Values From The Dictionary
    console.Write(dict(1).Name) ' Prints "Item 1"
    console.Write(dict(1).OtherValue) ' Prints "Item 1 Value"
End Sub

Ottenere valori distinti (usando il metodo Distinct)

Dim duplicateFruits = New List(Of String) From {"Grape", "Apple", "Grape", "Apple", "Grape"}
'At this point, duplicateFruits.Length = 5

Dim uniqueFruits = duplicateFruits.Distinct();
'Now, uniqueFruits.Count() = 2
'If iterated over at this point, it will contain 1 each of "Grape" and "Apple"


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow