LINQ Lambda Expressions in VB.NET - Querying Data


Server Intellect


LINQ Lambda Expressions in VB.NET - Querying Data

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.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
snack machines said:

I am trying to build a program to run a snack machine. I think I have it figured out but am still having a couple problems. How hard is this to integrate?

Posted 07/27/2010 at 2:11 PM
Flyer Design said:

I would wish every thanks giving day is celebrated that way never fail!! I just loved the pictures and can make out how cool it would have been!

Posted 07/31/2010 at 2:16 PM
Business Gifts said:

I was just browsing for relevant blog posts for my project research and I happened to stumble upon yours. Thanks for the excellent information.

Posted 07/31/2010 at 2:17 PM
SEO Consultant said:

This is my first time i visit here. I found so many interesting stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work.

Posted 07/31/2010 at 2:17 PM
UGG Bailey Button & UGG Bailey Button Boots said:

handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.

Let's start with the statusstrip label.Thanks

Posted 08/26/2010 at 8:17 AM
UGG Bailey Button Triplet said:

handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:20 AM
ugg classic boots & womens ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:22 AM
ugg classic boots & ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:24 AM
ugg classic boots & ugg boots said:

Let's start with the statusstrip label.Thanks

Posted 08/26/2010 at 8:25 AM
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:

with the statusstrip label.Thanks

Posted 08/26/2010 at 8:27 AM
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM

Posted 08/26/2010 at 8:28 AM
coach outlet said:

coach outlet

Posted 08/26/2010 at 1:55 PM
coach outlet said:

coach outlet

Posted 08/26/2010 at 1:55 PM
coach outlet said:

coach outlet

Posted 08/26/2010 at 1:55 PM
louis vuitton bags said:

louis vuitton bags

Posted 08/26/2010 at 1:56 PM
coach outlet store online said:
Posted 08/26/2010 at 9:17 PM

Leave a Comment