Introduction to LINQ: Using Collection in LINQ using ASP.NET and C#


Server Intellect


Introduction to LINQ: Using Collection in LINQ using ASP.NET and C#

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);
}
}
}

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:

thank you for sharing

Posted 08/26/2010 at 8:11 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: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 store online said:
Posted 08/26/2010 at 9:13 PM

Leave a Comment