Start ORM with LINQ in .Net Framework
In this post, I’m gonna show you how you can kick start your development with LINQ, new extension to .NET Framework that encompasses language-integrated query, set, and transform operations.
If you are unfamiliar with the terms ORM and LINQ then refer to the following articles to help you understand the idea and the purpose behind them.
I have heard of LINQ years back but did not have a chance to give it a test-drive since its first release. For your information LINQ was first introduced in .NET 3.0 and had been evangelized long before its public release.
Without going further into unnecessary explanation, I’m going to start guiding you on how to start LINQ in .Net.
To make things simple and easy to follow, let’s first create a console application project from your Visual Studio. Once the new project is created, then add a new Reference System.Data.Linq into the project from the Solution Explorer under .NET tab. If you are unable to find the specified reference then .NET 3.0 Framework is not yet installed on your PC. Please install the required Framework prior to proceeding to the next steps below. You may then verify the new reference under References tree node in your Solution Explorer.
Next is to create a simple class to represent a Person which, for the sake of simplicity, only consists of several attributes like Title, Name, Country. I have created a sample class for your reference as shown below.
using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
namespace JLoeConsole
{
[Table(Name = "Person")]
class Person
{
private string _title;
[Column(Storage = "_title", Name = "Title")]
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _name;
[Column(Storage = "_name", Name = "Name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _country;
[Column(Storage = "_country", Name = "Country")]
public string Country
{
get { return _country; }
set { _country = value; }
}
}
}
As you may notice, I have included a reference System.Data.Linq.Mapping into the class header. This is to enable the use of attribute [Table(Name = "Person")] as well as [Column(Storage = "_title", Name = "Title")].
[Table(Name = "Person")] is used to let LINQ know that the class Person maps to a database table named “Person” whereas [Column(Storage = "_title", Name = "Title")] tells LINQ to store the value of a column “Title” in the table “Person” into a variable named “_title”. Notice that _title property is also declared in the class as the storage. The same explanation goes to the rest of the properties.
At this juncture, we just completed the creation of a simple mapping class or ORM class.
Next step is to open the default driver class Program.cs and complete the project with the following codes. The codes need to be inserted in the driver function static void Main(string[] args){ ... }.
// Set your connection string below.
string connectionString = @"";
// Initialize DataContext class for LINQ
// with the specified connection string
DataContext db = new DataContext(connectionString);
// Create a new instance of Table class
// to contain list of persons
Table<Person> Persons = db.GetTable<Person>();
// Below is the Lambda expressions for LINQ
// to search people who live in Singapore
var persons =
from p in Persons
where p.Country == "SINGAPORE"
select p;
// Print names of all the person who stay in Singapore
foreach (Person person in persons)
{
Console.WriteLine(person.Name);
}
I hope the comments inside the codes above are sufficient to help you understand. Just in case you’re not sure what to fill in for your connection string you may refer to this site for clue.
Well I can say that, with these codes inside your project, the application is now ready for your build or debug. Hopefully you don’t encounter any issue following these steps and this example gives you a good start into your exploration of LINQ.
Last but not least, since Christmas and New year is just a few days away from the time I’m writing this post, I would like to wish you a Wonderful Christmas and Joyful New Year. Happy Holiday! :)
I would also like to give my token of appreciation to the following sites.
Don't leave just yet! You may also be interested to take a quick look at my other posts.
