Introduction to LINQ: Filtering Names from String Array in VB


Server Intellect


Introduction to LINQ: Filtering Names from String Array in VB

This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

In this introduction, we will introduce you to LINQ in a Windows Form. We are going to use LINQ to get and filter data from a String Array.

The first thing we do after installing the LINQ release above, is to start a new project > Visual Basic LINQ Windows Application.

In this example, we are going to create a string array with a list of names for the data source. Then we are going to filter the data by the length of the names, giving the user the chance to set the number of letters. So we will add a textbox, two labels, a button and a list box.
The form should look something like this:

Now we have our form, we can add the logic to the code-behind. Under the button click event we add the following:

Dim names() As String = {"Michael", "Tina", "Zach", "Ella", "Olivia", "Anthony", "Andrew"}
Dim namesWithFourCharacters As IEnumerable(Of String) = From name In names _
Where Name.Length < Convert.ToInt16(TextBox1.Text.ToString()) _
Select Name
lstNames.Items.Clear()
For Each Name As String In namesWithFourCharacters
lstNames.Items.Add(Name)
Next Name

Label1.Text = "Names with letters less than " & TextBox1.Text & ":"

This code creates a string array of names, and then the LINQ code selects only the names with the length (number of letters) less than what the user entered. The list box is cleared before data is entered, to make sure that it's new data each time, and then each name that matches the query is added to the list box.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

We can also add some code to make sure the user enters text. The entire code-behind will look something like this:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
Label1.Text = "Please enter a number."
Else
Dim names() As String = {"Michael", "Tina", "Zach", "Ella", "Olivia", "Anthony", "Andrew"}
Dim namesWithFourCharacters As IEnumerable(Of String) = From name In names _
Where Name.Length < Convert.ToInt16(TextBox1.Text.ToString()) _
Select Name
lstNames.Items.Clear()
For Each Name As String In namesWithFourCharacters
lstNames.Items.Add(Name)
Next Name

Label1.Text = "Names with letters less than " & TextBox1.Text & ":"
End If
End Sub
End Class

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
coach outlet said:

We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.

Posted 08/26/2010 at 8:10 AM
louis vuitton bags said:

We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.

Posted 08/26/2010 at 8:10 AM
louis vuitton bags & lv handbags & louis vuitton & lv said:

else

{

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM

Posted 08/26/2010 at 8:14 AM
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:23 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:26 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 store online said:
Posted 08/26/2010 at 9:12 PM

Leave a Comment