This tutorial was created with Microsoft Visual Studio .NET 2008, which comes with LINQ built-in. If you are using Visual Studio .NET 2005, you can still use LINQ by downloading Microsoft's LINQ Community Technology Preview release from here.
In this tutorial, we will look at how we can use LINQ to create quick classes, and allow LINQ to generate the Properties for us. Traditionally, we'd spend a lot of time coding the classes to represent our database data, but now, we can use LINQ to help us in creating these classes - and also with instantiating as well.
Let's start by creating a new web application in Visual Studio. We will create a new class - goto the Solution Explorer, right-click on the Project and choose to Add ASP.NET Folder > App_Code. Then right-click on the new App_Code folder and choose to Add New Item.. Class. Give it a descriptive name - for this example, we choose People.cs
Now we should have something that looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for People
/// </summary>
public class People
{
public People()
{
//
// TODO: Add constructor logic here
//
}
} |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
We can get rid of the constructor, as we will not be using that to create a new instance. To use Automatic Properties, we do not add logic to the Getters and Setters, we want the C# compiler to do this for us. NOTE: it is not possible to create Read-Only Automatic Properties. A Normal Property that we're used to creating may look something like this:
public Int32 ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
private Int32 _ID = 0; |
Believe it or not, we can minimize this down into one line of code, using Automatic Properties. This will look something like this:
| public Int32 ID { get; set; } |
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 do this for all of our Properties:
public Int32 ID { get; set; }
public String Name { get; set; }
public String City { get; set; }
public Int16 Age { get; set; } |
Now to create a new instance of this class, we can do it the old way - or the new LINQ way. The old way will look something like this:
People person1 = new People();
person1.ID = 1;
person1.Name = "Fred daRedd";
person1.City = "Fort Lauderdale";
person1.Age = 34; |
We can condense this down to one line using LINQ like so:
| People person2 = new People {ID = 2, Name = "Alanis Morieuvue", City = "Bratislav", Age = 24}; |
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.
Finally, to demonstrate our new class instances, we can output the properties to our ASPX page using a Literal control:
lit_Display.Text = "<strong>Person1:</strong> " + person1.Name + ", " + person1.City + " (" + person1.Age.ToString() + ")<br />";
lit_Display.Text += "<strong>Person2:</strong> " + person2.Name + ", " + person2.City + " (" + person2.Age.ToString() + ")<br />"; |
You can see how easy LINQ and the new features of the C# language make it for us to not only create our own classes, but to instantiate and use them as well.
Our ASPX page has just the one Literal control to output the properties:
| <form id="form1" runat="server">
<asp:Literal ID="lit_Display" runat="server" />
</form> |
The output to the webpage will look like this:
Person1: Fred daRedd, Fort Lauderdale (34)
Person2: Alanis Morieuvue, Bratislav (24) |
Finally, our entire code-behind looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
People person1 = new People();
person1.ID = 1;
person1.Name = "Fred daRedd";
person1.City = "Fort Lauderdale";
person1.Age = 34;
People person2 = new People {ID = 2, Name = "Alanis Morieuvue", City = "Bratislav", Age = 24};
lit_Display.Text = "<strong>Person1:</strong> " + person1.Name + ", " + person1.City + " (" + person1.Age.ToString() + ")<br />";
lit_Display.Text += "<strong>Person2:</strong> " + person2.Name + ", " + person2.City + " (" + person2.Age.ToString() + ")<br />";
}
} |