This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here
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.
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 > C# LINQ Windows Application.
You will notice the following assemblies have been referenced:
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq; |
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:
string[] names = {"Michael", "Tina", "Zach", "Ella", "Olivia", "Anthony", "Andrew"};
IEnumerable<string> namesWithFourCharacters =
from name in names
where name.Length < Convert.ToInt16(textBox1.Text.ToString())
select name;
lstNames.Items.Clear();
foreach(var name in namesWithFourCharacters)
lstNames.Items.Add(name);
label1.Text = "Names with letters less than " + textBox1.Text + ":"; |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
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 can also add some code to make sure the user enters text. The entire code-behind will look something like this;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace LINQ_Intro1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
label1.Text = "Please enter a number.";
}
else
{
string[] names = {"Michael", "Tina", "Zach", "Ella", "Olivia", "Anthony", "Andrew"};
IEnumerable<string> namesWithFourCharacters =
from name in names
where name.Length < Convert.ToInt16(textBox1.Text.ToString())
select name;
lstNames.Items.Clear();
foreach(var name in namesWithFourCharacters)
lstNames.Items.Add(name);
label1.Text = "Names with letters less than " + textBox1.Text + ":";
}
}
}
} |
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!