This tutorial was created with Visual Studio .NET 2008, but the results can be achieved in 2005 if Microsoft's LINQ Community Technology Preview release is downloaded and installed from Microsoft.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
In this tutorial, we will be looking at VB.NET Lambda Expressions. We will see how we can sort data from an array using the newly-introduced Lambda Expressions in VB.NET. Lambda Expressions can be used in place of LINQ Queries, and can make the code a lot shorter. In this example, we will be creating an array and retrieving data from the array depending on user entry. We will create an array of names, then retrieve only the ones that start with a certain letter.
We will be creating this example in a Windows Form. We will have one textbox for data input, one button to initiate the call, and one label to display the output. Once we have these controls on our form, we can begin coding. We can code everything into our OnClick button event.
First, we will initialize the string array. Then we can add the FOR loop to display our selection:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arrNames() As String = {"Mike", "Zach", "Ella", "Eli", "Jo"}
For Each varName In arrNames.Where(Function(n) n.StartsWith(TextBox1.Text))
Label2.Text = Label2.Text + varName & ", "
Next varName
End Sub |
The string is defined and some names are added to it. We then initialize a FOR loop and select the data we want: this Lambda Expression is selecting all the names within the array that begin with the text that was submitted to the textbox - if nothing is entered, everything will be returned and displayed.
We could do this another way, using a LINQ query, which is a slightly longer method:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nameResults = From name In arrNames _
Where name.StartsWith(TextBox1.Text) _
Select name
For Each varName In nameResults
Label2.Text = Label2.Text + varName & ", "
Next varName
End Sub |
To make the code more usable, we could add a try and catch to catch any exceptions, automatically change the text input to uppercase, and also clear the label before each request for data:
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.ToUpper()
Label2.Text = ""
Try
Dim arrNames() As String = {"Mike", "Zach", "Ella", "Eli", "Jo"}
For Each varName In arrNames.Where(Function(n) n.StartsWith(TextBox1.Text))
'Dim nameResults = From name In arrNames _
'Where name.StartsWith(TextBox1.Text) _
'Select Name
'For Each varName In nameResults
Label2.Text = Label2.Text + varName & ", "
Next varName
If Label2.Text = "" Then
Label2.Text = "Sorry, no results."
End If
Catch
Label2.Text = "Unable to process request, please try again."
End Try
End Sub |
The entire code-behind looks like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.ToUpper()
Label2.Text = ""
Try
Dim arrNames() As String = {"Mike", "Zach", "Ella", "Eli", "Jo"}
For Each varName In arrNames.Where(Function(n) n.StartsWith(TextBox1.Text))
'Dim nameResults = From name In arrNames _
'Where name.StartsWith(TextBox1.Text) _
'Select Name
'For Each varName In nameResults
Label2.Text = Label2.Text + varName & ", "
Next varName
If Label2.Text = "" Then
Label2.Text = "Sorry, no results."
End If
Catch
Label2.Text = "Unable to process request, please try again."
End Try
End Sub
End Class |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.