Friday, October 24, 2008

Playtime: I love Linq and WPF!

Wow, I had some free time today, and a funny idea for a new project. So I decided to learn a little about WPF and Linq and why they are so powerful. Let me tell you things like this:


public static void SignOut(User newUser)
{
WorkAchievementsDataContext db = new WorkAchievementsDataContext();

var updateUser = db.Users.First(u => u.ID == newUser.ID);

updateUser.IsLoggedOn = false;

db.SubmitChanges();
}


So entirely make my day!

Isn't that wonderful? Four lines of code and I have successfully updated my database. No connection strings, no stored procedures, it's just there in those four little lines of code! How wonderful. :)

Basically (for those that are new like myself) the lines do the following:

1) Creates an instance of the DataContext from my Linq to Sql Class.
2) Uses a lambda expression to retrieve the user that has an ID matching the one that is passed in.
3) Changes the "IsLoggedIn" field to false in my table.
4) Saves the changes to the database.

Being new at WPF and Linq, I am sure that I will find lots of fun and exciting things to play with, but for now it is time to go home.

0 comments: