05 November 2007

Playing with LINQ

This is the second post about LINQ, and this time I'm doing it seriously.

As seen on the previous post, LINQ is one of the features of .NET Framework 3.5 which allows programmers to use query statements (such as select, join or where) on C# or VB.NET code. On this post I'll show how to use LINQ with a database and display data on a web UI.

I have created a simple database named BlogSampleLINQ with 4 tables: User, Post, Profile and Category. I know this isn't how a blog database should look like, but this is just a post for example purposes.



The next thing I have done was to create a new project on Web Developer 2008, named BlogSampleLINQ.


The first thing I have done on the project was to add a new Item, which is a LINQ to SQL class. This class type will allow me to do a simple drag-n-drop on my tables and turn them into objects; I'll no longer have to worry about the connection and everything. It will create a database conext object which will handle it all to me. Nice :D


Now that I have dragged the tables into LINQ to SQL designer, I can do some changes to my objects, such as property names, data types and everything. On this example, I'll just change the name of 2 properties and the rest will be default. The LINQ designer looks like this:



Now let's start coding. I have created a new class to perform the queries on my brand-new objects with LINQ and wrote the following code:


The code above has a join with tables User and Profile of my SampleBlogLINQ database. The database is represented by the object sample, which is an instance of the DataContext. It binds the actual database with the objects I have created when dragged the tables to the LINQ designer. Note that on the select clause, I'm selecting the Id and DisplayName properties of the object u (which is an instance of User) and I'm also selecting a property named Profile. Well, profile is the name of one of my tables (and one of my objects too), but in this case it doesn't represent the object. It's just an alias for the object p (which is an instance of Profile object). I could call this property anything, and it would work fine. Think of it as a pointer to the real object Profile.

After having the code done and compiled, I can go back to the asp.net design and start doing the UI. For this example I have added a GridView control and an ObjectDataSource control. On the ObjectDataSource I have chosen the the BlogUsers class as my business object.

And then I have chosen a method of my class to be the Select method of the ObjectDataSource. Then only thing left now is to configure the columns. On the grid view, click on smart tab and click Edit Column. Then uncheck the option "auto-generate fileds". Add two BoundFields and one TemplateField. For the two first columns edit the property "DataField" with the name of the corresponding property. For each field, edit the "HeaderText". You won't edit the template field on this box. Just click ok.

Now click again on the smart tab of the GridView, but this time choose the option Edit Templates.


It will show a box with a single Label control. This control is bound with the ObjectDataSource, so you can click on it's smart tag and select the (only) option Edit Databindings. This is the only "hard" part. You are gonna have to remember what the property and the child property names are. Then you're gonna type them on the expression on custom binding. On my case, my Select method is returning an AnonymusType which has 3 properties: Id(int), Name(string) and Profile(Profile). I want to display the profile name on this field, therefore, I'll have to use the expression Eval("Profile.Name"), coz the profile name is a property of the property Profile of my type.


And that's it. By pressing F5, my sample application will run and it will display the users and their profiles.

Well, needless to say, LINQ is one of the best things ever made (I'm talking about programming only, of course). It's one of the strongest parts of Framework 3.5 and, as shown here, very simple to use. I intend to explore this small database on the next posts with examples about LINQ.

Just one more thing. A friend of mine is linking on his blog the poster of .NET Framework 3.5 in PDF format. This poster shows the whole structure of the upcoming framework. Click here to get it.

No comments: