#26: VB 2008 e LINQ to Objects

LINQ to Objects permette di eseguire interrogazioni su oggetti presenti in memoria.

Questa tecnica può essere applicata anche a un array, come possiamo vedere dal seguente esempio:

Public Class Form1 
    Private Sub Button1_Click(ByVal sender _ 
          As System.Object, _ 
          ByVal e As System.EventArgs) _ 
          Handles Button1.Click 
        ' variabili per composizione
        ' del messaggio finale: 
        Dim messaggio As String = "" 
        Dim NL As String = Environment.NewLine 
        ' dichiarazione array: 
        Dim frutti() As String = _ 
           {"mela", "pera", "banana", "mandarino"} 
        ' dichiarazione della variabile che conterrà
        ' l'insieme delle stringhe restituite dalla
        ' query. La query è dichiarata nella stessa
        ' istruzione: 
        Dim fruttiPreferiti = _ 
           From f In frutti _ 
           Where f.StartsWith("m") _ 
           Select f 
        ' scorrimento dell'insieme delle stringhe
        ' per la composizione del messaggio: 
        For Each frutto In fruttiPreferiti 
            messaggio &= "Frutto: " & frutto & NL 
        Next 
        MessageBox.Show(messaggio) 
    End Sub 
End Class

Le stringhe restituite dalla query sono “mela” e “mandarino”.

Posted on 5 marzo 2008, in LINQ, Tips and tagged , . Bookmark the permalink. Lascia un commento.

Lascia un Commento

Fill in your details below or click an icon to log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Log Out / Modifica )

Foto Twitter

You are commenting using your Twitter account. Log Out / Modifica )

Foto di Facebook

You are commenting using your Facebook account. Log Out / Modifica )

Connecting to %s

Iscriviti

Get every new post delivered to your Inbox.

Join 211 other followers