Since starting my journey into seeing what LiNQ can do I have been amazed. It is truly amazing and this will definitely shorten development time on so many different projects! Here's what I did tonight and you decide for yourself whether LiNQ is going to be useful or not.
I first created my database tables and set the relationship between the tables. Once you have done this, from your database server tab in Visual Studio, you simply drag and drop both tables into the LiNQ to SQL designer in Visual studio. This is what it looked like :

Once I had saved the dbml file I made a very simple ASP.Net Page for my Task Entry. I included a RadionButtonList to list all of the users in the database. Unfortunately you cannot bind directly to a linq table but I believe this is for security purposes. Once I can find the reason I will definitely post it because personally I feel one should be allowed to bind directly to a linq table. Once the page had been completed it looks like this:

Now as I previously stated, you cannot behind directly onto the linq table so you have to retrieve all the users and loop through them and add them individually to the RadioButtonList. Here is how I did it. This is by no means the perfect way of doing it but if you do know of a better way please let me know! This is the only way we learn is by learning from eachother!

TaskDataContext is the name that I gave to my LiNQ to SQL dbml class. You have to declare this to do any work on the database. This is basically your connection to your Database. Once we have our connection we want to retrieve all our users. I have declared a variable called _UserList to hold the list of users. The "var" keyword is new to Framework 3. The "var" keyword represents the "Implicitly Typed Local Variables" feature. The way this works is that when you declare the variable you don't need to specify the type *BUT* the variable *MUST* be initialized on declaration. The compiler will interpret the value and assign its data type accordingly.
Once the information is retrieved from the database that _UserList variable is assigned to a List<USER> variable containing a generic collection of USER classes. I think the rest of the code is self explanatory. I loop through the collection creating a new list item for each USER in the _UserList collection and add those items to the RadioButtonList.
Now comes the magic. Adding the Task to the database. This is the part of LiNQ that has absolutely amazed me thus far. We write the code for the "Add Task" button:

This is it! No SQL code typed whatsoever! No Insert SPROC needed. No Inline SQL needed, nothing! We simply declare our TasksDataContext and then declare a new task. We set the properties needed by the class and add the new TASK class to our TaskContext and then call the SubmitChanges() Method. Lets have a look whats in the database shall we?

And there we go. And I'll say it again, NO SQL CODE NEEDED FOR THE INSERT!!!!! I realize this makes me seem incredibly lazy which I am. Please feel free to correct anything I might've done wrong in your opinion or something I could've done better.
Next Article will cover Updating and Deleting using LiNQ.
Cross Posted from http://www.StevenMcD.Net/
Blogged with Flock