This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here
In this introduction, we will introduce you to using LINQ Collections. We will create a DropDownList, a Button and a ListBox. We will allow the user to select a City from the DropDownList. We will then filter the collection we create to match the user input, and then display the results in the ListBox.
This example was created as a web application, so note the assembly references at the top of the page:
using System.Query;
using System.Data.DLinq; |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
First, we add the Controls to the web form:
<form id="form1" runat="server">
Select a City to view people from there:
<asp:DropDownList ID="ListBox2" runat="server">
<asp:ListItem Selected="true">Orlando</asp:ListItem>
<asp:ListItem>New York</asp:ListItem>
<asp:ListItem>Miami</asp:ListItem>
<asp:ListItem>Richmond</asp:ListItem>
<asp:ListItem>Chicago</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" /><br /><br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
</form> |
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.
Next, we will create a Class named Person, which will allow us to retrieve and set data to our collection.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Person
/// </summary>
public class Person
{
private string _firstName;
private string _surName;
private string _city;
public Person()
{
//
// TODO: Add constructor logic here
//
}
public string FirstName
{
get { return _firstName; }
set{_firstName = value; }
}
public string Surname
{
get { return _surName; }
set { _surName = value; }
}
public string City
{
get{return _city; }
set{ _city = value; }
}
} |
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.
Now we are going to add logic to the code-behind. We will create a collection, and then compare the data to the user-submitted City and then output the matches to the ListBox.
We will use this class in the code-behind of our ASPX page like so:
using System;
using System.Data;
using System.Data.DLinq;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Query;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<Person> persons = new List<Person>
{new Person{FirstName = "Tina", Surname = "Smith", City = "Orlando"},
new Person{FirstName = "Michael", Surname ="Cox", City = "Orlando"},
new Person{FirstName = "Dave", Surname = "Arnold", City = "New York"},
new Person{FirstName = "Zach", Surname = "Simpson", City = "Chicago"},
new Person{FirstName = "Donald", Surname = "Ferguson", City = "Miami"},
new Person{FirstName = "Ella", Surname ="Gerrard", City = "Richmond"}};
var personsView = from person in persons
where person.City == ListBox2.SelectedItem.ToString()
orderby person.FirstName
select person;
ListBox1.Items.Clear();
foreach (var person in personsView)
{
ListBox1.Items.Add(person.FirstName + " " + person.Surname + " - " + person.City);
}
}
} |