Tuesday, December 30, 2008

Linq Projection in C#

In this tutorial we will look at LINQ Projection, which is when we can select specific data from a source without retrieving all fields. We will be creating a class to define a list in which we will create a number of people with IDs, names and cities. Then we will use buttons to select only parts of this data.

First, we will start off by creating a new Windows Form application in VS.NET 2008. Next, we will create a class - call it aList - and define our list object:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQProjection_cs
{
class aList
{
private int _personID;
private string _name;
private string _city;

public int PersonID
{
get
{
return _personID;
}
set
{
_personID = value;
}
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public string City
{
get
{
return _city;
}
set
{
_city = value;
}
}
}
}

This class defines a Property for each field we want, and its data type.
Next, we can add our Controls to the Form. We will add three buttons, and a Rich TextBox. The buttons will retrieve all of the IDs, Names and Cities, individually. This will demonstrates how we can retrieve the specific data that we want. Once we have our controls, we can move onto the code-behind of the form and define our data. We will add a few sample entries:

Please vist LinqHelp.com to complete this article. Happy coding!

No comments:

Post a Comment